mcbsp.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361
  1. /*
  2. * linux/arch/arm/plat-omap/mcbsp.c
  3. *
  4. * Copyright (C) 2004 Nokia Corporation
  5. * Author: Samuel Ortiz <samuel.ortiz@nokia.com>
  6. *
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * Multichannel mode not supported.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/device.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/err.h>
  20. #include <linux/clk.h>
  21. #include <linux/delay.h>
  22. #include <linux/io.h>
  23. #include <linux/slab.h>
  24. #include <plat/mcbsp.h>
  25. #include <linux/pm_runtime.h>
  26. /* XXX These "sideways" includes are a sign that something is wrong */
  27. #include "../mach-omap2/cm2xxx_3xxx.h"
  28. #include "../mach-omap2/cm-regbits-34xx.h"
  29. struct omap_mcbsp **mcbsp_ptr;
  30. int omap_mcbsp_count, omap_mcbsp_cache_size;
  31. static void omap_mcbsp_write(struct omap_mcbsp *mcbsp, u16 reg, u32 val)
  32. {
  33. void __iomem *addr = mcbsp->io_base + reg * mcbsp->pdata->reg_step;
  34. if (mcbsp->pdata->reg_size == 2) {
  35. ((u16 *)mcbsp->reg_cache)[reg] = (u16)val;
  36. __raw_writew((u16)val, addr);
  37. } else {
  38. ((u32 *)mcbsp->reg_cache)[reg] = val;
  39. __raw_writel(val, addr);
  40. }
  41. }
  42. static int omap_mcbsp_read(struct omap_mcbsp *mcbsp, u16 reg, bool from_cache)
  43. {
  44. void __iomem *addr = mcbsp->io_base + reg * mcbsp->pdata->reg_step;
  45. if (mcbsp->pdata->reg_size == 2) {
  46. return !from_cache ? __raw_readw(addr) :
  47. ((u16 *)mcbsp->reg_cache)[reg];
  48. } else {
  49. return !from_cache ? __raw_readl(addr) :
  50. ((u32 *)mcbsp->reg_cache)[reg];
  51. }
  52. }
  53. #ifdef CONFIG_ARCH_OMAP3
  54. static void omap_mcbsp_st_write(struct omap_mcbsp *mcbsp, u16 reg, u32 val)
  55. {
  56. __raw_writel(val, mcbsp->st_data->io_base_st + reg);
  57. }
  58. static int omap_mcbsp_st_read(struct omap_mcbsp *mcbsp, u16 reg)
  59. {
  60. return __raw_readl(mcbsp->st_data->io_base_st + reg);
  61. }
  62. #endif
  63. #define MCBSP_READ(mcbsp, reg) \
  64. omap_mcbsp_read(mcbsp, OMAP_MCBSP_REG_##reg, 0)
  65. #define MCBSP_WRITE(mcbsp, reg, val) \
  66. omap_mcbsp_write(mcbsp, OMAP_MCBSP_REG_##reg, val)
  67. #define MCBSP_READ_CACHE(mcbsp, reg) \
  68. omap_mcbsp_read(mcbsp, OMAP_MCBSP_REG_##reg, 1)
  69. #define MCBSP_ST_READ(mcbsp, reg) \
  70. omap_mcbsp_st_read(mcbsp, OMAP_ST_REG_##reg)
  71. #define MCBSP_ST_WRITE(mcbsp, reg, val) \
  72. omap_mcbsp_st_write(mcbsp, OMAP_ST_REG_##reg, val)
  73. static void omap_mcbsp_dump_reg(u8 id)
  74. {
  75. struct omap_mcbsp *mcbsp = id_to_mcbsp_ptr(id);
  76. dev_dbg(mcbsp->dev, "**** McBSP%d regs ****\n", mcbsp->id);
  77. dev_dbg(mcbsp->dev, "DRR2: 0x%04x\n",
  78. MCBSP_READ(mcbsp, DRR2));
  79. dev_dbg(mcbsp->dev, "DRR1: 0x%04x\n",
  80. MCBSP_READ(mcbsp, DRR1));
  81. dev_dbg(mcbsp->dev, "DXR2: 0x%04x\n",
  82. MCBSP_READ(mcbsp, DXR2));
  83. dev_dbg(mcbsp->dev, "DXR1: 0x%04x\n",
  84. MCBSP_READ(mcbsp, DXR1));
  85. dev_dbg(mcbsp->dev, "SPCR2: 0x%04x\n",
  86. MCBSP_READ(mcbsp, SPCR2));
  87. dev_dbg(mcbsp->dev, "SPCR1: 0x%04x\n",
  88. MCBSP_READ(mcbsp, SPCR1));
  89. dev_dbg(mcbsp->dev, "RCR2: 0x%04x\n",
  90. MCBSP_READ(mcbsp, RCR2));
  91. dev_dbg(mcbsp->dev, "RCR1: 0x%04x\n",
  92. MCBSP_READ(mcbsp, RCR1));
  93. dev_dbg(mcbsp->dev, "XCR2: 0x%04x\n",
  94. MCBSP_READ(mcbsp, XCR2));
  95. dev_dbg(mcbsp->dev, "XCR1: 0x%04x\n",
  96. MCBSP_READ(mcbsp, XCR1));
  97. dev_dbg(mcbsp->dev, "SRGR2: 0x%04x\n",
  98. MCBSP_READ(mcbsp, SRGR2));
  99. dev_dbg(mcbsp->dev, "SRGR1: 0x%04x\n",
  100. MCBSP_READ(mcbsp, SRGR1));
  101. dev_dbg(mcbsp->dev, "PCR0: 0x%04x\n",
  102. MCBSP_READ(mcbsp, PCR0));
  103. dev_dbg(mcbsp->dev, "***********************\n");
  104. }
  105. static irqreturn_t omap_mcbsp_tx_irq_handler(int irq, void *dev_id)
  106. {
  107. struct omap_mcbsp *mcbsp_tx = dev_id;
  108. u16 irqst_spcr2;
  109. irqst_spcr2 = MCBSP_READ(mcbsp_tx, SPCR2);
  110. dev_dbg(mcbsp_tx->dev, "TX IRQ callback : 0x%x\n", irqst_spcr2);
  111. if (irqst_spcr2 & XSYNC_ERR) {
  112. dev_err(mcbsp_tx->dev, "TX Frame Sync Error! : 0x%x\n",
  113. irqst_spcr2);
  114. /* Writing zero to XSYNC_ERR clears the IRQ */
  115. MCBSP_WRITE(mcbsp_tx, SPCR2, MCBSP_READ_CACHE(mcbsp_tx, SPCR2));
  116. }
  117. return IRQ_HANDLED;
  118. }
  119. static irqreturn_t omap_mcbsp_rx_irq_handler(int irq, void *dev_id)
  120. {
  121. struct omap_mcbsp *mcbsp_rx = dev_id;
  122. u16 irqst_spcr1;
  123. irqst_spcr1 = MCBSP_READ(mcbsp_rx, SPCR1);
  124. dev_dbg(mcbsp_rx->dev, "RX IRQ callback : 0x%x\n", irqst_spcr1);
  125. if (irqst_spcr1 & RSYNC_ERR) {
  126. dev_err(mcbsp_rx->dev, "RX Frame Sync Error! : 0x%x\n",
  127. irqst_spcr1);
  128. /* Writing zero to RSYNC_ERR clears the IRQ */
  129. MCBSP_WRITE(mcbsp_rx, SPCR1, MCBSP_READ_CACHE(mcbsp_rx, SPCR1));
  130. }
  131. return IRQ_HANDLED;
  132. }
  133. /*
  134. * omap_mcbsp_config simply write a config to the
  135. * appropriate McBSP.
  136. * You either call this function or set the McBSP registers
  137. * by yourself before calling omap_mcbsp_start().
  138. */
  139. void omap_mcbsp_config(unsigned int id, const struct omap_mcbsp_reg_cfg *config)
  140. {
  141. struct omap_mcbsp *mcbsp;
  142. if (!omap_mcbsp_check_valid_id(id)) {
  143. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  144. return;
  145. }
  146. mcbsp = id_to_mcbsp_ptr(id);
  147. dev_dbg(mcbsp->dev, "Configuring McBSP%d phys_base: 0x%08lx\n",
  148. mcbsp->id, mcbsp->phys_base);
  149. /* We write the given config */
  150. MCBSP_WRITE(mcbsp, SPCR2, config->spcr2);
  151. MCBSP_WRITE(mcbsp, SPCR1, config->spcr1);
  152. MCBSP_WRITE(mcbsp, RCR2, config->rcr2);
  153. MCBSP_WRITE(mcbsp, RCR1, config->rcr1);
  154. MCBSP_WRITE(mcbsp, XCR2, config->xcr2);
  155. MCBSP_WRITE(mcbsp, XCR1, config->xcr1);
  156. MCBSP_WRITE(mcbsp, SRGR2, config->srgr2);
  157. MCBSP_WRITE(mcbsp, SRGR1, config->srgr1);
  158. MCBSP_WRITE(mcbsp, MCR2, config->mcr2);
  159. MCBSP_WRITE(mcbsp, MCR1, config->mcr1);
  160. MCBSP_WRITE(mcbsp, PCR0, config->pcr0);
  161. if (mcbsp->pdata->has_ccr) {
  162. MCBSP_WRITE(mcbsp, XCCR, config->xccr);
  163. MCBSP_WRITE(mcbsp, RCCR, config->rccr);
  164. }
  165. }
  166. EXPORT_SYMBOL(omap_mcbsp_config);
  167. /**
  168. * omap_mcbsp_dma_params - returns the dma channel number
  169. * @id - mcbsp id
  170. * @stream - indicates the direction of data flow (rx or tx)
  171. *
  172. * Returns the dma channel number for the rx channel or tx channel
  173. * based on the value of @stream for the requested mcbsp given by @id
  174. */
  175. int omap_mcbsp_dma_ch_params(unsigned int id, unsigned int stream)
  176. {
  177. struct omap_mcbsp *mcbsp;
  178. if (!omap_mcbsp_check_valid_id(id)) {
  179. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  180. return -ENODEV;
  181. }
  182. mcbsp = id_to_mcbsp_ptr(id);
  183. if (stream)
  184. return mcbsp->dma_rx_sync;
  185. else
  186. return mcbsp->dma_tx_sync;
  187. }
  188. EXPORT_SYMBOL(omap_mcbsp_dma_ch_params);
  189. /**
  190. * omap_mcbsp_dma_reg_params - returns the address of mcbsp data register
  191. * @id - mcbsp id
  192. * @stream - indicates the direction of data flow (rx or tx)
  193. *
  194. * Returns the address of mcbsp data transmit register or data receive register
  195. * to be used by DMA for transferring/receiving data based on the value of
  196. * @stream for the requested mcbsp given by @id
  197. */
  198. int omap_mcbsp_dma_reg_params(unsigned int id, unsigned int stream)
  199. {
  200. struct omap_mcbsp *mcbsp;
  201. int data_reg;
  202. if (!omap_mcbsp_check_valid_id(id)) {
  203. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  204. return -ENODEV;
  205. }
  206. mcbsp = id_to_mcbsp_ptr(id);
  207. if (mcbsp->pdata->reg_size == 2) {
  208. if (stream)
  209. data_reg = OMAP_MCBSP_REG_DRR1;
  210. else
  211. data_reg = OMAP_MCBSP_REG_DXR1;
  212. } else {
  213. if (stream)
  214. data_reg = OMAP_MCBSP_REG_DRR;
  215. else
  216. data_reg = OMAP_MCBSP_REG_DXR;
  217. }
  218. return mcbsp->phys_dma_base + data_reg * mcbsp->pdata->reg_step;
  219. }
  220. EXPORT_SYMBOL(omap_mcbsp_dma_reg_params);
  221. #ifdef CONFIG_ARCH_OMAP3
  222. static void omap_st_on(struct omap_mcbsp *mcbsp)
  223. {
  224. unsigned int w;
  225. /*
  226. * Sidetone uses McBSP ICLK - which must not idle when sidetones
  227. * are enabled or sidetones start sounding ugly.
  228. */
  229. w = omap2_cm_read_mod_reg(OMAP3430_PER_MOD, CM_AUTOIDLE);
  230. w &= ~(1 << (mcbsp->id - 2));
  231. omap2_cm_write_mod_reg(w, OMAP3430_PER_MOD, CM_AUTOIDLE);
  232. /* Enable McBSP Sidetone */
  233. w = MCBSP_READ(mcbsp, SSELCR);
  234. MCBSP_WRITE(mcbsp, SSELCR, w | SIDETONEEN);
  235. /* Enable Sidetone from Sidetone Core */
  236. w = MCBSP_ST_READ(mcbsp, SSELCR);
  237. MCBSP_ST_WRITE(mcbsp, SSELCR, w | ST_SIDETONEEN);
  238. }
  239. static void omap_st_off(struct omap_mcbsp *mcbsp)
  240. {
  241. unsigned int w;
  242. w = MCBSP_ST_READ(mcbsp, SSELCR);
  243. MCBSP_ST_WRITE(mcbsp, SSELCR, w & ~(ST_SIDETONEEN));
  244. w = MCBSP_READ(mcbsp, SSELCR);
  245. MCBSP_WRITE(mcbsp, SSELCR, w & ~(SIDETONEEN));
  246. w = omap2_cm_read_mod_reg(OMAP3430_PER_MOD, CM_AUTOIDLE);
  247. w |= 1 << (mcbsp->id - 2);
  248. omap2_cm_write_mod_reg(w, OMAP3430_PER_MOD, CM_AUTOIDLE);
  249. }
  250. static void omap_st_fir_write(struct omap_mcbsp *mcbsp, s16 *fir)
  251. {
  252. u16 val, i;
  253. val = MCBSP_ST_READ(mcbsp, SSELCR);
  254. if (val & ST_COEFFWREN)
  255. MCBSP_ST_WRITE(mcbsp, SSELCR, val & ~(ST_COEFFWREN));
  256. MCBSP_ST_WRITE(mcbsp, SSELCR, val | ST_COEFFWREN);
  257. for (i = 0; i < 128; i++)
  258. MCBSP_ST_WRITE(mcbsp, SFIRCR, fir[i]);
  259. i = 0;
  260. val = MCBSP_ST_READ(mcbsp, SSELCR);
  261. while (!(val & ST_COEFFWRDONE) && (++i < 1000))
  262. val = MCBSP_ST_READ(mcbsp, SSELCR);
  263. MCBSP_ST_WRITE(mcbsp, SSELCR, val & ~(ST_COEFFWREN));
  264. if (i == 1000)
  265. dev_err(mcbsp->dev, "McBSP FIR load error!\n");
  266. }
  267. static void omap_st_chgain(struct omap_mcbsp *mcbsp)
  268. {
  269. u16 w;
  270. struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
  271. w = MCBSP_ST_READ(mcbsp, SSELCR);
  272. MCBSP_ST_WRITE(mcbsp, SGAINCR, ST_CH0GAIN(st_data->ch0gain) | \
  273. ST_CH1GAIN(st_data->ch1gain));
  274. }
  275. int omap_st_set_chgain(unsigned int id, int channel, s16 chgain)
  276. {
  277. struct omap_mcbsp *mcbsp;
  278. struct omap_mcbsp_st_data *st_data;
  279. int ret = 0;
  280. if (!omap_mcbsp_check_valid_id(id)) {
  281. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  282. return -ENODEV;
  283. }
  284. mcbsp = id_to_mcbsp_ptr(id);
  285. st_data = mcbsp->st_data;
  286. if (!st_data)
  287. return -ENOENT;
  288. spin_lock_irq(&mcbsp->lock);
  289. if (channel == 0)
  290. st_data->ch0gain = chgain;
  291. else if (channel == 1)
  292. st_data->ch1gain = chgain;
  293. else
  294. ret = -EINVAL;
  295. if (st_data->enabled)
  296. omap_st_chgain(mcbsp);
  297. spin_unlock_irq(&mcbsp->lock);
  298. return ret;
  299. }
  300. EXPORT_SYMBOL(omap_st_set_chgain);
  301. int omap_st_get_chgain(unsigned int id, int channel, s16 *chgain)
  302. {
  303. struct omap_mcbsp *mcbsp;
  304. struct omap_mcbsp_st_data *st_data;
  305. int ret = 0;
  306. if (!omap_mcbsp_check_valid_id(id)) {
  307. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  308. return -ENODEV;
  309. }
  310. mcbsp = id_to_mcbsp_ptr(id);
  311. st_data = mcbsp->st_data;
  312. if (!st_data)
  313. return -ENOENT;
  314. spin_lock_irq(&mcbsp->lock);
  315. if (channel == 0)
  316. *chgain = st_data->ch0gain;
  317. else if (channel == 1)
  318. *chgain = st_data->ch1gain;
  319. else
  320. ret = -EINVAL;
  321. spin_unlock_irq(&mcbsp->lock);
  322. return ret;
  323. }
  324. EXPORT_SYMBOL(omap_st_get_chgain);
  325. static int omap_st_start(struct omap_mcbsp *mcbsp)
  326. {
  327. struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
  328. if (st_data && st_data->enabled && !st_data->running) {
  329. omap_st_fir_write(mcbsp, st_data->taps);
  330. omap_st_chgain(mcbsp);
  331. if (!mcbsp->free) {
  332. omap_st_on(mcbsp);
  333. st_data->running = 1;
  334. }
  335. }
  336. return 0;
  337. }
  338. int omap_st_enable(unsigned int id)
  339. {
  340. struct omap_mcbsp *mcbsp;
  341. struct omap_mcbsp_st_data *st_data;
  342. if (!omap_mcbsp_check_valid_id(id)) {
  343. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  344. return -ENODEV;
  345. }
  346. mcbsp = id_to_mcbsp_ptr(id);
  347. st_data = mcbsp->st_data;
  348. if (!st_data)
  349. return -ENODEV;
  350. spin_lock_irq(&mcbsp->lock);
  351. st_data->enabled = 1;
  352. omap_st_start(mcbsp);
  353. spin_unlock_irq(&mcbsp->lock);
  354. return 0;
  355. }
  356. EXPORT_SYMBOL(omap_st_enable);
  357. static int omap_st_stop(struct omap_mcbsp *mcbsp)
  358. {
  359. struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
  360. if (st_data && st_data->running) {
  361. if (!mcbsp->free) {
  362. omap_st_off(mcbsp);
  363. st_data->running = 0;
  364. }
  365. }
  366. return 0;
  367. }
  368. int omap_st_disable(unsigned int id)
  369. {
  370. struct omap_mcbsp *mcbsp;
  371. struct omap_mcbsp_st_data *st_data;
  372. int ret = 0;
  373. if (!omap_mcbsp_check_valid_id(id)) {
  374. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  375. return -ENODEV;
  376. }
  377. mcbsp = id_to_mcbsp_ptr(id);
  378. st_data = mcbsp->st_data;
  379. if (!st_data)
  380. return -ENODEV;
  381. spin_lock_irq(&mcbsp->lock);
  382. omap_st_stop(mcbsp);
  383. st_data->enabled = 0;
  384. spin_unlock_irq(&mcbsp->lock);
  385. return ret;
  386. }
  387. EXPORT_SYMBOL(omap_st_disable);
  388. int omap_st_is_enabled(unsigned int id)
  389. {
  390. struct omap_mcbsp *mcbsp;
  391. struct omap_mcbsp_st_data *st_data;
  392. if (!omap_mcbsp_check_valid_id(id)) {
  393. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  394. return -ENODEV;
  395. }
  396. mcbsp = id_to_mcbsp_ptr(id);
  397. st_data = mcbsp->st_data;
  398. if (!st_data)
  399. return -ENODEV;
  400. return st_data->enabled;
  401. }
  402. EXPORT_SYMBOL(omap_st_is_enabled);
  403. /*
  404. * omap_mcbsp_set_rx_threshold configures the transmit threshold in words.
  405. * The threshold parameter is 1 based, and it is converted (threshold - 1)
  406. * for the THRSH2 register.
  407. */
  408. void omap_mcbsp_set_tx_threshold(unsigned int id, u16 threshold)
  409. {
  410. struct omap_mcbsp *mcbsp;
  411. if (!cpu_is_omap34xx() && !cpu_is_omap44xx())
  412. return;
  413. if (!omap_mcbsp_check_valid_id(id)) {
  414. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  415. return;
  416. }
  417. mcbsp = id_to_mcbsp_ptr(id);
  418. if (threshold && threshold <= mcbsp->max_tx_thres)
  419. MCBSP_WRITE(mcbsp, THRSH2, threshold - 1);
  420. }
  421. EXPORT_SYMBOL(omap_mcbsp_set_tx_threshold);
  422. /*
  423. * omap_mcbsp_set_rx_threshold configures the receive threshold in words.
  424. * The threshold parameter is 1 based, and it is converted (threshold - 1)
  425. * for the THRSH1 register.
  426. */
  427. void omap_mcbsp_set_rx_threshold(unsigned int id, u16 threshold)
  428. {
  429. struct omap_mcbsp *mcbsp;
  430. if (!cpu_is_omap34xx() && !cpu_is_omap44xx())
  431. return;
  432. if (!omap_mcbsp_check_valid_id(id)) {
  433. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  434. return;
  435. }
  436. mcbsp = id_to_mcbsp_ptr(id);
  437. if (threshold && threshold <= mcbsp->max_rx_thres)
  438. MCBSP_WRITE(mcbsp, THRSH1, threshold - 1);
  439. }
  440. EXPORT_SYMBOL(omap_mcbsp_set_rx_threshold);
  441. /*
  442. * omap_mcbsp_get_max_tx_thres just return the current configured
  443. * maximum threshold for transmission
  444. */
  445. u16 omap_mcbsp_get_max_tx_threshold(unsigned int id)
  446. {
  447. struct omap_mcbsp *mcbsp;
  448. if (!omap_mcbsp_check_valid_id(id)) {
  449. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  450. return -ENODEV;
  451. }
  452. mcbsp = id_to_mcbsp_ptr(id);
  453. return mcbsp->max_tx_thres;
  454. }
  455. EXPORT_SYMBOL(omap_mcbsp_get_max_tx_threshold);
  456. /*
  457. * omap_mcbsp_get_max_rx_thres just return the current configured
  458. * maximum threshold for reception
  459. */
  460. u16 omap_mcbsp_get_max_rx_threshold(unsigned int id)
  461. {
  462. struct omap_mcbsp *mcbsp;
  463. if (!omap_mcbsp_check_valid_id(id)) {
  464. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  465. return -ENODEV;
  466. }
  467. mcbsp = id_to_mcbsp_ptr(id);
  468. return mcbsp->max_rx_thres;
  469. }
  470. EXPORT_SYMBOL(omap_mcbsp_get_max_rx_threshold);
  471. u16 omap_mcbsp_get_fifo_size(unsigned int id)
  472. {
  473. struct omap_mcbsp *mcbsp;
  474. if (!omap_mcbsp_check_valid_id(id)) {
  475. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  476. return -ENODEV;
  477. }
  478. mcbsp = id_to_mcbsp_ptr(id);
  479. return mcbsp->pdata->buffer_size;
  480. }
  481. EXPORT_SYMBOL(omap_mcbsp_get_fifo_size);
  482. /*
  483. * omap_mcbsp_get_tx_delay returns the number of used slots in the McBSP FIFO
  484. */
  485. u16 omap_mcbsp_get_tx_delay(unsigned int id)
  486. {
  487. struct omap_mcbsp *mcbsp;
  488. u16 buffstat;
  489. if (!omap_mcbsp_check_valid_id(id)) {
  490. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  491. return -ENODEV;
  492. }
  493. mcbsp = id_to_mcbsp_ptr(id);
  494. /* Returns the number of free locations in the buffer */
  495. buffstat = MCBSP_READ(mcbsp, XBUFFSTAT);
  496. /* Number of slots are different in McBSP ports */
  497. return mcbsp->pdata->buffer_size - buffstat;
  498. }
  499. EXPORT_SYMBOL(omap_mcbsp_get_tx_delay);
  500. /*
  501. * omap_mcbsp_get_rx_delay returns the number of free slots in the McBSP FIFO
  502. * to reach the threshold value (when the DMA will be triggered to read it)
  503. */
  504. u16 omap_mcbsp_get_rx_delay(unsigned int id)
  505. {
  506. struct omap_mcbsp *mcbsp;
  507. u16 buffstat, threshold;
  508. if (!omap_mcbsp_check_valid_id(id)) {
  509. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  510. return -ENODEV;
  511. }
  512. mcbsp = id_to_mcbsp_ptr(id);
  513. /* Returns the number of used locations in the buffer */
  514. buffstat = MCBSP_READ(mcbsp, RBUFFSTAT);
  515. /* RX threshold */
  516. threshold = MCBSP_READ(mcbsp, THRSH1);
  517. /* Return the number of location till we reach the threshold limit */
  518. if (threshold <= buffstat)
  519. return 0;
  520. else
  521. return threshold - buffstat;
  522. }
  523. EXPORT_SYMBOL(omap_mcbsp_get_rx_delay);
  524. /*
  525. * omap_mcbsp_get_dma_op_mode just return the current configured
  526. * operating mode for the mcbsp channel
  527. */
  528. int omap_mcbsp_get_dma_op_mode(unsigned int id)
  529. {
  530. struct omap_mcbsp *mcbsp;
  531. int dma_op_mode;
  532. if (!omap_mcbsp_check_valid_id(id)) {
  533. printk(KERN_ERR "%s: Invalid id (%u)\n", __func__, id + 1);
  534. return -ENODEV;
  535. }
  536. mcbsp = id_to_mcbsp_ptr(id);
  537. dma_op_mode = mcbsp->dma_op_mode;
  538. return dma_op_mode;
  539. }
  540. EXPORT_SYMBOL(omap_mcbsp_get_dma_op_mode);
  541. #else
  542. static inline void omap_st_start(struct omap_mcbsp *mcbsp) {}
  543. static inline void omap_st_stop(struct omap_mcbsp *mcbsp) {}
  544. #endif
  545. int omap_mcbsp_request(unsigned int id)
  546. {
  547. struct omap_mcbsp *mcbsp;
  548. void *reg_cache;
  549. int err;
  550. if (!omap_mcbsp_check_valid_id(id)) {
  551. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  552. return -ENODEV;
  553. }
  554. mcbsp = id_to_mcbsp_ptr(id);
  555. reg_cache = kzalloc(omap_mcbsp_cache_size, GFP_KERNEL);
  556. if (!reg_cache) {
  557. return -ENOMEM;
  558. }
  559. spin_lock(&mcbsp->lock);
  560. if (!mcbsp->free) {
  561. dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
  562. mcbsp->id);
  563. err = -EBUSY;
  564. goto err_kfree;
  565. }
  566. mcbsp->free = false;
  567. mcbsp->reg_cache = reg_cache;
  568. spin_unlock(&mcbsp->lock);
  569. if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->request)
  570. mcbsp->pdata->ops->request(id);
  571. pm_runtime_get_sync(mcbsp->dev);
  572. /* Enable wakeup behavior */
  573. if (mcbsp->pdata->has_wakeup)
  574. MCBSP_WRITE(mcbsp, WAKEUPEN, XRDYEN | RRDYEN);
  575. /*
  576. * Make sure that transmitter, receiver and sample-rate generator are
  577. * not running before activating IRQs.
  578. */
  579. MCBSP_WRITE(mcbsp, SPCR1, 0);
  580. MCBSP_WRITE(mcbsp, SPCR2, 0);
  581. err = request_irq(mcbsp->tx_irq, omap_mcbsp_tx_irq_handler,
  582. 0, "McBSP", (void *)mcbsp);
  583. if (err != 0) {
  584. dev_err(mcbsp->dev, "Unable to request TX IRQ %d "
  585. "for McBSP%d\n", mcbsp->tx_irq,
  586. mcbsp->id);
  587. goto err_clk_disable;
  588. }
  589. if (mcbsp->rx_irq) {
  590. err = request_irq(mcbsp->rx_irq,
  591. omap_mcbsp_rx_irq_handler,
  592. 0, "McBSP", (void *)mcbsp);
  593. if (err != 0) {
  594. dev_err(mcbsp->dev, "Unable to request RX IRQ %d "
  595. "for McBSP%d\n", mcbsp->rx_irq,
  596. mcbsp->id);
  597. goto err_free_irq;
  598. }
  599. }
  600. return 0;
  601. err_free_irq:
  602. free_irq(mcbsp->tx_irq, (void *)mcbsp);
  603. err_clk_disable:
  604. if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
  605. mcbsp->pdata->ops->free(id);
  606. /* Disable wakeup behavior */
  607. if (mcbsp->pdata->has_wakeup)
  608. MCBSP_WRITE(mcbsp, WAKEUPEN, 0);
  609. pm_runtime_put_sync(mcbsp->dev);
  610. spin_lock(&mcbsp->lock);
  611. mcbsp->free = true;
  612. mcbsp->reg_cache = NULL;
  613. err_kfree:
  614. spin_unlock(&mcbsp->lock);
  615. kfree(reg_cache);
  616. return err;
  617. }
  618. EXPORT_SYMBOL(omap_mcbsp_request);
  619. void omap_mcbsp_free(unsigned int id)
  620. {
  621. struct omap_mcbsp *mcbsp;
  622. void *reg_cache;
  623. if (!omap_mcbsp_check_valid_id(id)) {
  624. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  625. return;
  626. }
  627. mcbsp = id_to_mcbsp_ptr(id);
  628. if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
  629. mcbsp->pdata->ops->free(id);
  630. /* Disable wakeup behavior */
  631. if (mcbsp->pdata->has_wakeup)
  632. MCBSP_WRITE(mcbsp, WAKEUPEN, 0);
  633. pm_runtime_put_sync(mcbsp->dev);
  634. if (mcbsp->rx_irq)
  635. free_irq(mcbsp->rx_irq, (void *)mcbsp);
  636. free_irq(mcbsp->tx_irq, (void *)mcbsp);
  637. reg_cache = mcbsp->reg_cache;
  638. spin_lock(&mcbsp->lock);
  639. if (mcbsp->free)
  640. dev_err(mcbsp->dev, "McBSP%d was not reserved\n", mcbsp->id);
  641. else
  642. mcbsp->free = true;
  643. mcbsp->reg_cache = NULL;
  644. spin_unlock(&mcbsp->lock);
  645. if (reg_cache)
  646. kfree(reg_cache);
  647. }
  648. EXPORT_SYMBOL(omap_mcbsp_free);
  649. /*
  650. * Here we start the McBSP, by enabling transmitter, receiver or both.
  651. * If no transmitter or receiver is active prior calling, then sample-rate
  652. * generator and frame sync are started.
  653. */
  654. void omap_mcbsp_start(unsigned int id, int tx, int rx)
  655. {
  656. struct omap_mcbsp *mcbsp;
  657. int enable_srg = 0;
  658. u16 w;
  659. if (!omap_mcbsp_check_valid_id(id)) {
  660. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  661. return;
  662. }
  663. mcbsp = id_to_mcbsp_ptr(id);
  664. if (cpu_is_omap34xx())
  665. omap_st_start(mcbsp);
  666. /* Only enable SRG, if McBSP is master */
  667. w = MCBSP_READ_CACHE(mcbsp, PCR0);
  668. if (w & (FSXM | FSRM | CLKXM | CLKRM))
  669. enable_srg = !((MCBSP_READ_CACHE(mcbsp, SPCR2) |
  670. MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1);
  671. if (enable_srg) {
  672. /* Start the sample generator */
  673. w = MCBSP_READ_CACHE(mcbsp, SPCR2);
  674. MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 6));
  675. }
  676. /* Enable transmitter and receiver */
  677. tx &= 1;
  678. w = MCBSP_READ_CACHE(mcbsp, SPCR2);
  679. MCBSP_WRITE(mcbsp, SPCR2, w | tx);
  680. rx &= 1;
  681. w = MCBSP_READ_CACHE(mcbsp, SPCR1);
  682. MCBSP_WRITE(mcbsp, SPCR1, w | rx);
  683. /*
  684. * Worst case: CLKSRG*2 = 8000khz: (1/8000) * 2 * 2 usec
  685. * REVISIT: 100us may give enough time for two CLKSRG, however
  686. * due to some unknown PM related, clock gating etc. reason it
  687. * is now at 500us.
  688. */
  689. udelay(500);
  690. if (enable_srg) {
  691. /* Start frame sync */
  692. w = MCBSP_READ_CACHE(mcbsp, SPCR2);
  693. MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 7));
  694. }
  695. if (mcbsp->pdata->has_ccr) {
  696. /* Release the transmitter and receiver */
  697. w = MCBSP_READ_CACHE(mcbsp, XCCR);
  698. w &= ~(tx ? XDISABLE : 0);
  699. MCBSP_WRITE(mcbsp, XCCR, w);
  700. w = MCBSP_READ_CACHE(mcbsp, RCCR);
  701. w &= ~(rx ? RDISABLE : 0);
  702. MCBSP_WRITE(mcbsp, RCCR, w);
  703. }
  704. /* Dump McBSP Regs */
  705. omap_mcbsp_dump_reg(id);
  706. }
  707. EXPORT_SYMBOL(omap_mcbsp_start);
  708. void omap_mcbsp_stop(unsigned int id, int tx, int rx)
  709. {
  710. struct omap_mcbsp *mcbsp;
  711. int idle;
  712. u16 w;
  713. if (!omap_mcbsp_check_valid_id(id)) {
  714. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  715. return;
  716. }
  717. mcbsp = id_to_mcbsp_ptr(id);
  718. /* Reset transmitter */
  719. tx &= 1;
  720. if (mcbsp->pdata->has_ccr) {
  721. w = MCBSP_READ_CACHE(mcbsp, XCCR);
  722. w |= (tx ? XDISABLE : 0);
  723. MCBSP_WRITE(mcbsp, XCCR, w);
  724. }
  725. w = MCBSP_READ_CACHE(mcbsp, SPCR2);
  726. MCBSP_WRITE(mcbsp, SPCR2, w & ~tx);
  727. /* Reset receiver */
  728. rx &= 1;
  729. if (mcbsp->pdata->has_ccr) {
  730. w = MCBSP_READ_CACHE(mcbsp, RCCR);
  731. w |= (rx ? RDISABLE : 0);
  732. MCBSP_WRITE(mcbsp, RCCR, w);
  733. }
  734. w = MCBSP_READ_CACHE(mcbsp, SPCR1);
  735. MCBSP_WRITE(mcbsp, SPCR1, w & ~rx);
  736. idle = !((MCBSP_READ_CACHE(mcbsp, SPCR2) |
  737. MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1);
  738. if (idle) {
  739. /* Reset the sample rate generator */
  740. w = MCBSP_READ_CACHE(mcbsp, SPCR2);
  741. MCBSP_WRITE(mcbsp, SPCR2, w & ~(1 << 6));
  742. }
  743. if (cpu_is_omap34xx())
  744. omap_st_stop(mcbsp);
  745. }
  746. EXPORT_SYMBOL(omap_mcbsp_stop);
  747. /*
  748. * The following functions are only required on an OMAP1-only build.
  749. * mach-omap2/mcbsp.c contains the real functions
  750. */
  751. #ifndef CONFIG_ARCH_OMAP2PLUS
  752. int omap2_mcbsp_set_clks_src(u8 id, u8 fck_src_id)
  753. {
  754. WARN(1, "%s: should never be called on an OMAP1-only kernel\n",
  755. __func__);
  756. return -EINVAL;
  757. }
  758. void omap2_mcbsp1_mux_clkr_src(u8 mux)
  759. {
  760. WARN(1, "%s: should never be called on an OMAP1-only kernel\n",
  761. __func__);
  762. return;
  763. }
  764. void omap2_mcbsp1_mux_fsr_src(u8 mux)
  765. {
  766. WARN(1, "%s: should never be called on an OMAP1-only kernel\n",
  767. __func__);
  768. return;
  769. }
  770. #endif
  771. #ifdef CONFIG_ARCH_OMAP3
  772. #define max_thres(m) (mcbsp->pdata->buffer_size)
  773. #define valid_threshold(m, val) ((val) <= max_thres(m))
  774. #define THRESHOLD_PROP_BUILDER(prop) \
  775. static ssize_t prop##_show(struct device *dev, \
  776. struct device_attribute *attr, char *buf) \
  777. { \
  778. struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
  779. \
  780. return sprintf(buf, "%u\n", mcbsp->prop); \
  781. } \
  782. \
  783. static ssize_t prop##_store(struct device *dev, \
  784. struct device_attribute *attr, \
  785. const char *buf, size_t size) \
  786. { \
  787. struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
  788. unsigned long val; \
  789. int status; \
  790. \
  791. status = strict_strtoul(buf, 0, &val); \
  792. if (status) \
  793. return status; \
  794. \
  795. if (!valid_threshold(mcbsp, val)) \
  796. return -EDOM; \
  797. \
  798. mcbsp->prop = val; \
  799. return size; \
  800. } \
  801. \
  802. static DEVICE_ATTR(prop, 0644, prop##_show, prop##_store);
  803. THRESHOLD_PROP_BUILDER(max_tx_thres);
  804. THRESHOLD_PROP_BUILDER(max_rx_thres);
  805. static const char *dma_op_modes[] = {
  806. "element", "threshold", "frame",
  807. };
  808. static ssize_t dma_op_mode_show(struct device *dev,
  809. struct device_attribute *attr, char *buf)
  810. {
  811. struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
  812. int dma_op_mode, i = 0;
  813. ssize_t len = 0;
  814. const char * const *s;
  815. dma_op_mode = mcbsp->dma_op_mode;
  816. for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++) {
  817. if (dma_op_mode == i)
  818. len += sprintf(buf + len, "[%s] ", *s);
  819. else
  820. len += sprintf(buf + len, "%s ", *s);
  821. }
  822. len += sprintf(buf + len, "\n");
  823. return len;
  824. }
  825. static ssize_t dma_op_mode_store(struct device *dev,
  826. struct device_attribute *attr,
  827. const char *buf, size_t size)
  828. {
  829. struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
  830. const char * const *s;
  831. int i = 0;
  832. for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++)
  833. if (sysfs_streq(buf, *s))
  834. break;
  835. if (i == ARRAY_SIZE(dma_op_modes))
  836. return -EINVAL;
  837. spin_lock_irq(&mcbsp->lock);
  838. if (!mcbsp->free) {
  839. size = -EBUSY;
  840. goto unlock;
  841. }
  842. mcbsp->dma_op_mode = i;
  843. unlock:
  844. spin_unlock_irq(&mcbsp->lock);
  845. return size;
  846. }
  847. static DEVICE_ATTR(dma_op_mode, 0644, dma_op_mode_show, dma_op_mode_store);
  848. static ssize_t st_taps_show(struct device *dev,
  849. struct device_attribute *attr, char *buf)
  850. {
  851. struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
  852. struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
  853. ssize_t status = 0;
  854. int i;
  855. spin_lock_irq(&mcbsp->lock);
  856. for (i = 0; i < st_data->nr_taps; i++)
  857. status += sprintf(&buf[status], (i ? ", %d" : "%d"),
  858. st_data->taps[i]);
  859. if (i)
  860. status += sprintf(&buf[status], "\n");
  861. spin_unlock_irq(&mcbsp->lock);
  862. return status;
  863. }
  864. static ssize_t st_taps_store(struct device *dev,
  865. struct device_attribute *attr,
  866. const char *buf, size_t size)
  867. {
  868. struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
  869. struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
  870. int val, tmp, status, i = 0;
  871. spin_lock_irq(&mcbsp->lock);
  872. memset(st_data->taps, 0, sizeof(st_data->taps));
  873. st_data->nr_taps = 0;
  874. do {
  875. status = sscanf(buf, "%d%n", &val, &tmp);
  876. if (status < 0 || status == 0) {
  877. size = -EINVAL;
  878. goto out;
  879. }
  880. if (val < -32768 || val > 32767) {
  881. size = -EINVAL;
  882. goto out;
  883. }
  884. st_data->taps[i++] = val;
  885. buf += tmp;
  886. if (*buf != ',')
  887. break;
  888. buf++;
  889. } while (1);
  890. st_data->nr_taps = i;
  891. out:
  892. spin_unlock_irq(&mcbsp->lock);
  893. return size;
  894. }
  895. static DEVICE_ATTR(st_taps, 0644, st_taps_show, st_taps_store);
  896. static const struct attribute *additional_attrs[] = {
  897. &dev_attr_max_tx_thres.attr,
  898. &dev_attr_max_rx_thres.attr,
  899. &dev_attr_dma_op_mode.attr,
  900. NULL,
  901. };
  902. static const struct attribute_group additional_attr_group = {
  903. .attrs = (struct attribute **)additional_attrs,
  904. };
  905. static inline int __devinit omap_additional_add(struct device *dev)
  906. {
  907. return sysfs_create_group(&dev->kobj, &additional_attr_group);
  908. }
  909. static inline void __devexit omap_additional_remove(struct device *dev)
  910. {
  911. sysfs_remove_group(&dev->kobj, &additional_attr_group);
  912. }
  913. static const struct attribute *sidetone_attrs[] = {
  914. &dev_attr_st_taps.attr,
  915. NULL,
  916. };
  917. static const struct attribute_group sidetone_attr_group = {
  918. .attrs = (struct attribute **)sidetone_attrs,
  919. };
  920. static int __devinit omap_st_add(struct omap_mcbsp *mcbsp)
  921. {
  922. struct platform_device *pdev;
  923. struct resource *res;
  924. struct omap_mcbsp_st_data *st_data;
  925. int err;
  926. st_data = kzalloc(sizeof(*mcbsp->st_data), GFP_KERNEL);
  927. if (!st_data) {
  928. err = -ENOMEM;
  929. goto err1;
  930. }
  931. pdev = container_of(mcbsp->dev, struct platform_device, dev);
  932. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sidetone");
  933. st_data->io_base_st = ioremap(res->start, resource_size(res));
  934. if (!st_data->io_base_st) {
  935. err = -ENOMEM;
  936. goto err2;
  937. }
  938. err = sysfs_create_group(&mcbsp->dev->kobj, &sidetone_attr_group);
  939. if (err)
  940. goto err3;
  941. mcbsp->st_data = st_data;
  942. return 0;
  943. err3:
  944. iounmap(st_data->io_base_st);
  945. err2:
  946. kfree(st_data);
  947. err1:
  948. return err;
  949. }
  950. static void __devexit omap_st_remove(struct omap_mcbsp *mcbsp)
  951. {
  952. struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
  953. if (st_data) {
  954. sysfs_remove_group(&mcbsp->dev->kobj, &sidetone_attr_group);
  955. iounmap(st_data->io_base_st);
  956. kfree(st_data);
  957. }
  958. }
  959. static inline void __devinit omap34xx_device_init(struct omap_mcbsp *mcbsp)
  960. {
  961. mcbsp->dma_op_mode = MCBSP_DMA_MODE_ELEMENT;
  962. if (cpu_is_omap34xx()) {
  963. /*
  964. * Initially configure the maximum thresholds to a safe value.
  965. * The McBSP FIFO usage with these values should not go under
  966. * 16 locations.
  967. * If the whole FIFO without safety buffer is used, than there
  968. * is a possibility that the DMA will be not able to push the
  969. * new data on time, causing channel shifts in runtime.
  970. */
  971. mcbsp->max_tx_thres = max_thres(mcbsp) - 0x10;
  972. mcbsp->max_rx_thres = max_thres(mcbsp) - 0x10;
  973. /*
  974. * REVISIT: Set dmap_op_mode to THRESHOLD as default
  975. * for mcbsp2 instances.
  976. */
  977. if (omap_additional_add(mcbsp->dev))
  978. dev_warn(mcbsp->dev,
  979. "Unable to create additional controls\n");
  980. if (mcbsp->id == 2 || mcbsp->id == 3)
  981. if (omap_st_add(mcbsp))
  982. dev_warn(mcbsp->dev,
  983. "Unable to create sidetone controls\n");
  984. } else {
  985. mcbsp->max_tx_thres = -EINVAL;
  986. mcbsp->max_rx_thres = -EINVAL;
  987. }
  988. }
  989. static inline void __devexit omap34xx_device_exit(struct omap_mcbsp *mcbsp)
  990. {
  991. if (cpu_is_omap34xx()) {
  992. omap_additional_remove(mcbsp->dev);
  993. if (mcbsp->id == 2 || mcbsp->id == 3)
  994. omap_st_remove(mcbsp);
  995. }
  996. }
  997. #else
  998. static inline void __devinit omap34xx_device_init(struct omap_mcbsp *mcbsp) {}
  999. static inline void __devexit omap34xx_device_exit(struct omap_mcbsp *mcbsp) {}
  1000. #endif /* CONFIG_ARCH_OMAP3 */
  1001. /*
  1002. * McBSP1 and McBSP3 are directly mapped on 1610 and 1510.
  1003. * 730 has only 2 McBSP, and both of them are MPU peripherals.
  1004. */
  1005. static int __devinit omap_mcbsp_probe(struct platform_device *pdev)
  1006. {
  1007. struct omap_mcbsp_platform_data *pdata = pdev->dev.platform_data;
  1008. struct omap_mcbsp *mcbsp;
  1009. int id = pdev->id - 1;
  1010. struct resource *res;
  1011. int ret = 0;
  1012. if (!pdata) {
  1013. dev_err(&pdev->dev, "McBSP device initialized without"
  1014. "platform data\n");
  1015. ret = -EINVAL;
  1016. goto exit;
  1017. }
  1018. dev_dbg(&pdev->dev, "Initializing OMAP McBSP (%d).\n", pdev->id);
  1019. if (id >= omap_mcbsp_count) {
  1020. dev_err(&pdev->dev, "Invalid McBSP device id (%d)\n", id);
  1021. ret = -EINVAL;
  1022. goto exit;
  1023. }
  1024. mcbsp = kzalloc(sizeof(struct omap_mcbsp), GFP_KERNEL);
  1025. if (!mcbsp) {
  1026. ret = -ENOMEM;
  1027. goto exit;
  1028. }
  1029. spin_lock_init(&mcbsp->lock);
  1030. mcbsp->id = id + 1;
  1031. mcbsp->free = true;
  1032. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mpu");
  1033. if (!res) {
  1034. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1035. if (!res) {
  1036. dev_err(&pdev->dev, "%s:mcbsp%d has invalid memory"
  1037. "resource\n", __func__, pdev->id);
  1038. ret = -ENOMEM;
  1039. goto exit;
  1040. }
  1041. }
  1042. mcbsp->phys_base = res->start;
  1043. omap_mcbsp_cache_size = resource_size(res);
  1044. mcbsp->io_base = ioremap(res->start, resource_size(res));
  1045. if (!mcbsp->io_base) {
  1046. ret = -ENOMEM;
  1047. goto err_ioremap;
  1048. }
  1049. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dma");
  1050. if (!res)
  1051. mcbsp->phys_dma_base = mcbsp->phys_base;
  1052. else
  1053. mcbsp->phys_dma_base = res->start;
  1054. mcbsp->tx_irq = platform_get_irq_byname(pdev, "tx");
  1055. mcbsp->rx_irq = platform_get_irq_byname(pdev, "rx");
  1056. /* From OMAP4 there will be a single irq line */
  1057. if (mcbsp->tx_irq == -ENXIO)
  1058. mcbsp->tx_irq = platform_get_irq(pdev, 0);
  1059. res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
  1060. if (!res) {
  1061. dev_err(&pdev->dev, "%s:mcbsp%d has invalid rx DMA channel\n",
  1062. __func__, pdev->id);
  1063. ret = -ENODEV;
  1064. goto err_res;
  1065. }
  1066. mcbsp->dma_rx_sync = res->start;
  1067. res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
  1068. if (!res) {
  1069. dev_err(&pdev->dev, "%s:mcbsp%d has invalid tx DMA channel\n",
  1070. __func__, pdev->id);
  1071. ret = -ENODEV;
  1072. goto err_res;
  1073. }
  1074. mcbsp->dma_tx_sync = res->start;
  1075. mcbsp->fclk = clk_get(&pdev->dev, "fck");
  1076. if (IS_ERR(mcbsp->fclk)) {
  1077. ret = PTR_ERR(mcbsp->fclk);
  1078. dev_err(&pdev->dev, "unable to get fck: %d\n", ret);
  1079. goto err_res;
  1080. }
  1081. mcbsp->pdata = pdata;
  1082. mcbsp->dev = &pdev->dev;
  1083. mcbsp_ptr[id] = mcbsp;
  1084. platform_set_drvdata(pdev, mcbsp);
  1085. pm_runtime_enable(mcbsp->dev);
  1086. /* Initialize mcbsp properties for OMAP34XX if needed / applicable */
  1087. omap34xx_device_init(mcbsp);
  1088. return 0;
  1089. err_res:
  1090. iounmap(mcbsp->io_base);
  1091. err_ioremap:
  1092. kfree(mcbsp);
  1093. exit:
  1094. return ret;
  1095. }
  1096. static int __devexit omap_mcbsp_remove(struct platform_device *pdev)
  1097. {
  1098. struct omap_mcbsp *mcbsp = platform_get_drvdata(pdev);
  1099. platform_set_drvdata(pdev, NULL);
  1100. if (mcbsp) {
  1101. if (mcbsp->pdata && mcbsp->pdata->ops &&
  1102. mcbsp->pdata->ops->free)
  1103. mcbsp->pdata->ops->free(mcbsp->id);
  1104. omap34xx_device_exit(mcbsp);
  1105. clk_put(mcbsp->fclk);
  1106. iounmap(mcbsp->io_base);
  1107. kfree(mcbsp);
  1108. }
  1109. return 0;
  1110. }
  1111. static struct platform_driver omap_mcbsp_driver = {
  1112. .probe = omap_mcbsp_probe,
  1113. .remove = __devexit_p(omap_mcbsp_remove),
  1114. .driver = {
  1115. .name = "omap-mcbsp",
  1116. },
  1117. };
  1118. int __init omap_mcbsp_init(void)
  1119. {
  1120. /* Register the McBSP driver */
  1121. return platform_driver_register(&omap_mcbsp_driver);
  1122. }