mcbsp.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357
  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. #else
  404. static inline void omap_st_start(struct omap_mcbsp *mcbsp) {}
  405. static inline void omap_st_stop(struct omap_mcbsp *mcbsp) {}
  406. #endif
  407. /*
  408. * omap_mcbsp_set_rx_threshold configures the transmit threshold in words.
  409. * The threshold parameter is 1 based, and it is converted (threshold - 1)
  410. * for the THRSH2 register.
  411. */
  412. void omap_mcbsp_set_tx_threshold(unsigned int id, u16 threshold)
  413. {
  414. struct omap_mcbsp *mcbsp;
  415. if (!omap_mcbsp_check_valid_id(id)) {
  416. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  417. return;
  418. }
  419. mcbsp = id_to_mcbsp_ptr(id);
  420. if (mcbsp->pdata->buffer_size == 0)
  421. return;
  422. if (threshold && threshold <= mcbsp->max_tx_thres)
  423. MCBSP_WRITE(mcbsp, THRSH2, threshold - 1);
  424. }
  425. EXPORT_SYMBOL(omap_mcbsp_set_tx_threshold);
  426. /*
  427. * omap_mcbsp_set_rx_threshold configures the receive threshold in words.
  428. * The threshold parameter is 1 based, and it is converted (threshold - 1)
  429. * for the THRSH1 register.
  430. */
  431. void omap_mcbsp_set_rx_threshold(unsigned int id, u16 threshold)
  432. {
  433. struct omap_mcbsp *mcbsp;
  434. if (!omap_mcbsp_check_valid_id(id)) {
  435. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  436. return;
  437. }
  438. mcbsp = id_to_mcbsp_ptr(id);
  439. if (mcbsp->pdata->buffer_size == 0)
  440. return;
  441. if (threshold && threshold <= mcbsp->max_rx_thres)
  442. MCBSP_WRITE(mcbsp, THRSH1, threshold - 1);
  443. }
  444. EXPORT_SYMBOL(omap_mcbsp_set_rx_threshold);
  445. /*
  446. * omap_mcbsp_get_max_tx_thres just return the current configured
  447. * maximum threshold for transmission
  448. */
  449. u16 omap_mcbsp_get_max_tx_threshold(unsigned int id)
  450. {
  451. struct omap_mcbsp *mcbsp;
  452. if (!omap_mcbsp_check_valid_id(id)) {
  453. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  454. return -ENODEV;
  455. }
  456. mcbsp = id_to_mcbsp_ptr(id);
  457. return mcbsp->max_tx_thres;
  458. }
  459. EXPORT_SYMBOL(omap_mcbsp_get_max_tx_threshold);
  460. /*
  461. * omap_mcbsp_get_max_rx_thres just return the current configured
  462. * maximum threshold for reception
  463. */
  464. u16 omap_mcbsp_get_max_rx_threshold(unsigned int id)
  465. {
  466. struct omap_mcbsp *mcbsp;
  467. if (!omap_mcbsp_check_valid_id(id)) {
  468. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  469. return -ENODEV;
  470. }
  471. mcbsp = id_to_mcbsp_ptr(id);
  472. return mcbsp->max_rx_thres;
  473. }
  474. EXPORT_SYMBOL(omap_mcbsp_get_max_rx_threshold);
  475. u16 omap_mcbsp_get_fifo_size(unsigned int id)
  476. {
  477. struct omap_mcbsp *mcbsp;
  478. if (!omap_mcbsp_check_valid_id(id)) {
  479. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  480. return -ENODEV;
  481. }
  482. mcbsp = id_to_mcbsp_ptr(id);
  483. return mcbsp->pdata->buffer_size;
  484. }
  485. EXPORT_SYMBOL(omap_mcbsp_get_fifo_size);
  486. /*
  487. * omap_mcbsp_get_tx_delay returns the number of used slots in the McBSP FIFO
  488. */
  489. u16 omap_mcbsp_get_tx_delay(unsigned int id)
  490. {
  491. struct omap_mcbsp *mcbsp;
  492. u16 buffstat;
  493. if (!omap_mcbsp_check_valid_id(id)) {
  494. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  495. return -ENODEV;
  496. }
  497. mcbsp = id_to_mcbsp_ptr(id);
  498. if (mcbsp->pdata->buffer_size == 0)
  499. return 0;
  500. /* Returns the number of free locations in the buffer */
  501. buffstat = MCBSP_READ(mcbsp, XBUFFSTAT);
  502. /* Number of slots are different in McBSP ports */
  503. return mcbsp->pdata->buffer_size - buffstat;
  504. }
  505. EXPORT_SYMBOL(omap_mcbsp_get_tx_delay);
  506. /*
  507. * omap_mcbsp_get_rx_delay returns the number of free slots in the McBSP FIFO
  508. * to reach the threshold value (when the DMA will be triggered to read it)
  509. */
  510. u16 omap_mcbsp_get_rx_delay(unsigned int id)
  511. {
  512. struct omap_mcbsp *mcbsp;
  513. u16 buffstat, threshold;
  514. if (!omap_mcbsp_check_valid_id(id)) {
  515. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  516. return -ENODEV;
  517. }
  518. mcbsp = id_to_mcbsp_ptr(id);
  519. if (mcbsp->pdata->buffer_size == 0)
  520. return 0;
  521. /* Returns the number of used locations in the buffer */
  522. buffstat = MCBSP_READ(mcbsp, RBUFFSTAT);
  523. /* RX threshold */
  524. threshold = MCBSP_READ(mcbsp, THRSH1);
  525. /* Return the number of location till we reach the threshold limit */
  526. if (threshold <= buffstat)
  527. return 0;
  528. else
  529. return threshold - buffstat;
  530. }
  531. EXPORT_SYMBOL(omap_mcbsp_get_rx_delay);
  532. /*
  533. * omap_mcbsp_get_dma_op_mode just return the current configured
  534. * operating mode for the mcbsp channel
  535. */
  536. int omap_mcbsp_get_dma_op_mode(unsigned int id)
  537. {
  538. struct omap_mcbsp *mcbsp;
  539. int dma_op_mode;
  540. if (!omap_mcbsp_check_valid_id(id)) {
  541. printk(KERN_ERR "%s: Invalid id (%u)\n", __func__, id + 1);
  542. return -ENODEV;
  543. }
  544. mcbsp = id_to_mcbsp_ptr(id);
  545. dma_op_mode = mcbsp->dma_op_mode;
  546. return dma_op_mode;
  547. }
  548. EXPORT_SYMBOL(omap_mcbsp_get_dma_op_mode);
  549. int omap_mcbsp_request(unsigned int id)
  550. {
  551. struct omap_mcbsp *mcbsp;
  552. void *reg_cache;
  553. int err;
  554. if (!omap_mcbsp_check_valid_id(id)) {
  555. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  556. return -ENODEV;
  557. }
  558. mcbsp = id_to_mcbsp_ptr(id);
  559. reg_cache = kzalloc(omap_mcbsp_cache_size, GFP_KERNEL);
  560. if (!reg_cache) {
  561. return -ENOMEM;
  562. }
  563. spin_lock(&mcbsp->lock);
  564. if (!mcbsp->free) {
  565. dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
  566. mcbsp->id);
  567. err = -EBUSY;
  568. goto err_kfree;
  569. }
  570. mcbsp->free = false;
  571. mcbsp->reg_cache = reg_cache;
  572. spin_unlock(&mcbsp->lock);
  573. if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->request)
  574. mcbsp->pdata->ops->request(id);
  575. pm_runtime_get_sync(mcbsp->dev);
  576. /* Enable wakeup behavior */
  577. if (mcbsp->pdata->has_wakeup)
  578. MCBSP_WRITE(mcbsp, WAKEUPEN, XRDYEN | RRDYEN);
  579. /*
  580. * Make sure that transmitter, receiver and sample-rate generator are
  581. * not running before activating IRQs.
  582. */
  583. MCBSP_WRITE(mcbsp, SPCR1, 0);
  584. MCBSP_WRITE(mcbsp, SPCR2, 0);
  585. err = request_irq(mcbsp->tx_irq, omap_mcbsp_tx_irq_handler,
  586. 0, "McBSP", (void *)mcbsp);
  587. if (err != 0) {
  588. dev_err(mcbsp->dev, "Unable to request TX IRQ %d "
  589. "for McBSP%d\n", mcbsp->tx_irq,
  590. mcbsp->id);
  591. goto err_clk_disable;
  592. }
  593. if (mcbsp->rx_irq) {
  594. err = request_irq(mcbsp->rx_irq,
  595. omap_mcbsp_rx_irq_handler,
  596. 0, "McBSP", (void *)mcbsp);
  597. if (err != 0) {
  598. dev_err(mcbsp->dev, "Unable to request RX IRQ %d "
  599. "for McBSP%d\n", mcbsp->rx_irq,
  600. mcbsp->id);
  601. goto err_free_irq;
  602. }
  603. }
  604. return 0;
  605. err_free_irq:
  606. free_irq(mcbsp->tx_irq, (void *)mcbsp);
  607. err_clk_disable:
  608. if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
  609. mcbsp->pdata->ops->free(id);
  610. /* Disable wakeup behavior */
  611. if (mcbsp->pdata->has_wakeup)
  612. MCBSP_WRITE(mcbsp, WAKEUPEN, 0);
  613. pm_runtime_put_sync(mcbsp->dev);
  614. spin_lock(&mcbsp->lock);
  615. mcbsp->free = true;
  616. mcbsp->reg_cache = NULL;
  617. err_kfree:
  618. spin_unlock(&mcbsp->lock);
  619. kfree(reg_cache);
  620. return err;
  621. }
  622. EXPORT_SYMBOL(omap_mcbsp_request);
  623. void omap_mcbsp_free(unsigned int id)
  624. {
  625. struct omap_mcbsp *mcbsp;
  626. void *reg_cache;
  627. if (!omap_mcbsp_check_valid_id(id)) {
  628. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  629. return;
  630. }
  631. mcbsp = id_to_mcbsp_ptr(id);
  632. if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
  633. mcbsp->pdata->ops->free(id);
  634. /* Disable wakeup behavior */
  635. if (mcbsp->pdata->has_wakeup)
  636. MCBSP_WRITE(mcbsp, WAKEUPEN, 0);
  637. pm_runtime_put_sync(mcbsp->dev);
  638. if (mcbsp->rx_irq)
  639. free_irq(mcbsp->rx_irq, (void *)mcbsp);
  640. free_irq(mcbsp->tx_irq, (void *)mcbsp);
  641. reg_cache = mcbsp->reg_cache;
  642. spin_lock(&mcbsp->lock);
  643. if (mcbsp->free)
  644. dev_err(mcbsp->dev, "McBSP%d was not reserved\n", mcbsp->id);
  645. else
  646. mcbsp->free = true;
  647. mcbsp->reg_cache = NULL;
  648. spin_unlock(&mcbsp->lock);
  649. if (reg_cache)
  650. kfree(reg_cache);
  651. }
  652. EXPORT_SYMBOL(omap_mcbsp_free);
  653. /*
  654. * Here we start the McBSP, by enabling transmitter, receiver or both.
  655. * If no transmitter or receiver is active prior calling, then sample-rate
  656. * generator and frame sync are started.
  657. */
  658. void omap_mcbsp_start(unsigned int id, int tx, int rx)
  659. {
  660. struct omap_mcbsp *mcbsp;
  661. int enable_srg = 0;
  662. u16 w;
  663. if (!omap_mcbsp_check_valid_id(id)) {
  664. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  665. return;
  666. }
  667. mcbsp = id_to_mcbsp_ptr(id);
  668. if (cpu_is_omap34xx())
  669. omap_st_start(mcbsp);
  670. /* Only enable SRG, if McBSP is master */
  671. w = MCBSP_READ_CACHE(mcbsp, PCR0);
  672. if (w & (FSXM | FSRM | CLKXM | CLKRM))
  673. enable_srg = !((MCBSP_READ_CACHE(mcbsp, SPCR2) |
  674. MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1);
  675. if (enable_srg) {
  676. /* Start the sample generator */
  677. w = MCBSP_READ_CACHE(mcbsp, SPCR2);
  678. MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 6));
  679. }
  680. /* Enable transmitter and receiver */
  681. tx &= 1;
  682. w = MCBSP_READ_CACHE(mcbsp, SPCR2);
  683. MCBSP_WRITE(mcbsp, SPCR2, w | tx);
  684. rx &= 1;
  685. w = MCBSP_READ_CACHE(mcbsp, SPCR1);
  686. MCBSP_WRITE(mcbsp, SPCR1, w | rx);
  687. /*
  688. * Worst case: CLKSRG*2 = 8000khz: (1/8000) * 2 * 2 usec
  689. * REVISIT: 100us may give enough time for two CLKSRG, however
  690. * due to some unknown PM related, clock gating etc. reason it
  691. * is now at 500us.
  692. */
  693. udelay(500);
  694. if (enable_srg) {
  695. /* Start frame sync */
  696. w = MCBSP_READ_CACHE(mcbsp, SPCR2);
  697. MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 7));
  698. }
  699. if (mcbsp->pdata->has_ccr) {
  700. /* Release the transmitter and receiver */
  701. w = MCBSP_READ_CACHE(mcbsp, XCCR);
  702. w &= ~(tx ? XDISABLE : 0);
  703. MCBSP_WRITE(mcbsp, XCCR, w);
  704. w = MCBSP_READ_CACHE(mcbsp, RCCR);
  705. w &= ~(rx ? RDISABLE : 0);
  706. MCBSP_WRITE(mcbsp, RCCR, w);
  707. }
  708. /* Dump McBSP Regs */
  709. omap_mcbsp_dump_reg(id);
  710. }
  711. EXPORT_SYMBOL(omap_mcbsp_start);
  712. void omap_mcbsp_stop(unsigned int id, int tx, int rx)
  713. {
  714. struct omap_mcbsp *mcbsp;
  715. int idle;
  716. u16 w;
  717. if (!omap_mcbsp_check_valid_id(id)) {
  718. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  719. return;
  720. }
  721. mcbsp = id_to_mcbsp_ptr(id);
  722. /* Reset transmitter */
  723. tx &= 1;
  724. if (mcbsp->pdata->has_ccr) {
  725. w = MCBSP_READ_CACHE(mcbsp, XCCR);
  726. w |= (tx ? XDISABLE : 0);
  727. MCBSP_WRITE(mcbsp, XCCR, w);
  728. }
  729. w = MCBSP_READ_CACHE(mcbsp, SPCR2);
  730. MCBSP_WRITE(mcbsp, SPCR2, w & ~tx);
  731. /* Reset receiver */
  732. rx &= 1;
  733. if (mcbsp->pdata->has_ccr) {
  734. w = MCBSP_READ_CACHE(mcbsp, RCCR);
  735. w |= (rx ? RDISABLE : 0);
  736. MCBSP_WRITE(mcbsp, RCCR, w);
  737. }
  738. w = MCBSP_READ_CACHE(mcbsp, SPCR1);
  739. MCBSP_WRITE(mcbsp, SPCR1, w & ~rx);
  740. idle = !((MCBSP_READ_CACHE(mcbsp, SPCR2) |
  741. MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1);
  742. if (idle) {
  743. /* Reset the sample rate generator */
  744. w = MCBSP_READ_CACHE(mcbsp, SPCR2);
  745. MCBSP_WRITE(mcbsp, SPCR2, w & ~(1 << 6));
  746. }
  747. if (cpu_is_omap34xx())
  748. omap_st_stop(mcbsp);
  749. }
  750. EXPORT_SYMBOL(omap_mcbsp_stop);
  751. /*
  752. * The following functions are only required on an OMAP1-only build.
  753. * mach-omap2/mcbsp.c contains the real functions
  754. */
  755. #ifndef CONFIG_ARCH_OMAP2PLUS
  756. int omap2_mcbsp_set_clks_src(u8 id, u8 fck_src_id)
  757. {
  758. WARN(1, "%s: should never be called on an OMAP1-only kernel\n",
  759. __func__);
  760. return -EINVAL;
  761. }
  762. void omap2_mcbsp1_mux_clkr_src(u8 mux)
  763. {
  764. WARN(1, "%s: should never be called on an OMAP1-only kernel\n",
  765. __func__);
  766. return;
  767. }
  768. void omap2_mcbsp1_mux_fsr_src(u8 mux)
  769. {
  770. WARN(1, "%s: should never be called on an OMAP1-only kernel\n",
  771. __func__);
  772. return;
  773. }
  774. #endif
  775. #define max_thres(m) (mcbsp->pdata->buffer_size)
  776. #define valid_threshold(m, val) ((val) <= max_thres(m))
  777. #define THRESHOLD_PROP_BUILDER(prop) \
  778. static ssize_t prop##_show(struct device *dev, \
  779. struct device_attribute *attr, char *buf) \
  780. { \
  781. struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
  782. \
  783. return sprintf(buf, "%u\n", mcbsp->prop); \
  784. } \
  785. \
  786. static ssize_t prop##_store(struct device *dev, \
  787. struct device_attribute *attr, \
  788. const char *buf, size_t size) \
  789. { \
  790. struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
  791. unsigned long val; \
  792. int status; \
  793. \
  794. status = strict_strtoul(buf, 0, &val); \
  795. if (status) \
  796. return status; \
  797. \
  798. if (!valid_threshold(mcbsp, val)) \
  799. return -EDOM; \
  800. \
  801. mcbsp->prop = val; \
  802. return size; \
  803. } \
  804. \
  805. static DEVICE_ATTR(prop, 0644, prop##_show, prop##_store);
  806. THRESHOLD_PROP_BUILDER(max_tx_thres);
  807. THRESHOLD_PROP_BUILDER(max_rx_thres);
  808. static const char *dma_op_modes[] = {
  809. "element", "threshold", "frame",
  810. };
  811. static ssize_t dma_op_mode_show(struct device *dev,
  812. struct device_attribute *attr, char *buf)
  813. {
  814. struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
  815. int dma_op_mode, i = 0;
  816. ssize_t len = 0;
  817. const char * const *s;
  818. dma_op_mode = mcbsp->dma_op_mode;
  819. for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++) {
  820. if (dma_op_mode == i)
  821. len += sprintf(buf + len, "[%s] ", *s);
  822. else
  823. len += sprintf(buf + len, "%s ", *s);
  824. }
  825. len += sprintf(buf + len, "\n");
  826. return len;
  827. }
  828. static ssize_t dma_op_mode_store(struct device *dev,
  829. struct device_attribute *attr,
  830. const char *buf, size_t size)
  831. {
  832. struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
  833. const char * const *s;
  834. int i = 0;
  835. for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++)
  836. if (sysfs_streq(buf, *s))
  837. break;
  838. if (i == ARRAY_SIZE(dma_op_modes))
  839. return -EINVAL;
  840. spin_lock_irq(&mcbsp->lock);
  841. if (!mcbsp->free) {
  842. size = -EBUSY;
  843. goto unlock;
  844. }
  845. mcbsp->dma_op_mode = i;
  846. unlock:
  847. spin_unlock_irq(&mcbsp->lock);
  848. return size;
  849. }
  850. static DEVICE_ATTR(dma_op_mode, 0644, dma_op_mode_show, dma_op_mode_store);
  851. static const struct attribute *additional_attrs[] = {
  852. &dev_attr_max_tx_thres.attr,
  853. &dev_attr_max_rx_thres.attr,
  854. &dev_attr_dma_op_mode.attr,
  855. NULL,
  856. };
  857. static const struct attribute_group additional_attr_group = {
  858. .attrs = (struct attribute **)additional_attrs,
  859. };
  860. #ifdef CONFIG_ARCH_OMAP3
  861. static ssize_t st_taps_show(struct device *dev,
  862. struct device_attribute *attr, char *buf)
  863. {
  864. struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
  865. struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
  866. ssize_t status = 0;
  867. int i;
  868. spin_lock_irq(&mcbsp->lock);
  869. for (i = 0; i < st_data->nr_taps; i++)
  870. status += sprintf(&buf[status], (i ? ", %d" : "%d"),
  871. st_data->taps[i]);
  872. if (i)
  873. status += sprintf(&buf[status], "\n");
  874. spin_unlock_irq(&mcbsp->lock);
  875. return status;
  876. }
  877. static ssize_t st_taps_store(struct device *dev,
  878. struct device_attribute *attr,
  879. const char *buf, size_t size)
  880. {
  881. struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
  882. struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
  883. int val, tmp, status, i = 0;
  884. spin_lock_irq(&mcbsp->lock);
  885. memset(st_data->taps, 0, sizeof(st_data->taps));
  886. st_data->nr_taps = 0;
  887. do {
  888. status = sscanf(buf, "%d%n", &val, &tmp);
  889. if (status < 0 || status == 0) {
  890. size = -EINVAL;
  891. goto out;
  892. }
  893. if (val < -32768 || val > 32767) {
  894. size = -EINVAL;
  895. goto out;
  896. }
  897. st_data->taps[i++] = val;
  898. buf += tmp;
  899. if (*buf != ',')
  900. break;
  901. buf++;
  902. } while (1);
  903. st_data->nr_taps = i;
  904. out:
  905. spin_unlock_irq(&mcbsp->lock);
  906. return size;
  907. }
  908. static DEVICE_ATTR(st_taps, 0644, st_taps_show, st_taps_store);
  909. static const struct attribute *sidetone_attrs[] = {
  910. &dev_attr_st_taps.attr,
  911. NULL,
  912. };
  913. static const struct attribute_group sidetone_attr_group = {
  914. .attrs = (struct attribute **)sidetone_attrs,
  915. };
  916. static int __devinit omap_st_add(struct omap_mcbsp *mcbsp)
  917. {
  918. struct platform_device *pdev;
  919. struct resource *res;
  920. struct omap_mcbsp_st_data *st_data;
  921. int err;
  922. st_data = kzalloc(sizeof(*mcbsp->st_data), GFP_KERNEL);
  923. if (!st_data) {
  924. err = -ENOMEM;
  925. goto err1;
  926. }
  927. pdev = container_of(mcbsp->dev, struct platform_device, dev);
  928. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sidetone");
  929. st_data->io_base_st = ioremap(res->start, resource_size(res));
  930. if (!st_data->io_base_st) {
  931. err = -ENOMEM;
  932. goto err2;
  933. }
  934. err = sysfs_create_group(&mcbsp->dev->kobj, &sidetone_attr_group);
  935. if (err)
  936. goto err3;
  937. mcbsp->st_data = st_data;
  938. return 0;
  939. err3:
  940. iounmap(st_data->io_base_st);
  941. err2:
  942. kfree(st_data);
  943. err1:
  944. return err;
  945. }
  946. static void __devexit omap_st_remove(struct omap_mcbsp *mcbsp)
  947. {
  948. struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
  949. if (st_data) {
  950. sysfs_remove_group(&mcbsp->dev->kobj, &sidetone_attr_group);
  951. iounmap(st_data->io_base_st);
  952. kfree(st_data);
  953. }
  954. }
  955. static inline void __devinit omap34xx_device_init(struct omap_mcbsp *mcbsp)
  956. {
  957. if (cpu_is_omap34xx())
  958. if (mcbsp->id == 2 || mcbsp->id == 3)
  959. if (omap_st_add(mcbsp))
  960. dev_warn(mcbsp->dev,
  961. "Unable to create sidetone controls\n");
  962. }
  963. static inline void __devexit omap34xx_device_exit(struct omap_mcbsp *mcbsp)
  964. {
  965. if (cpu_is_omap34xx())
  966. if (mcbsp->id == 2 || mcbsp->id == 3)
  967. omap_st_remove(mcbsp);
  968. }
  969. #else
  970. static inline void __devinit omap34xx_device_init(struct omap_mcbsp *mcbsp) {}
  971. static inline void __devexit omap34xx_device_exit(struct omap_mcbsp *mcbsp) {}
  972. #endif /* CONFIG_ARCH_OMAP3 */
  973. /*
  974. * McBSP1 and McBSP3 are directly mapped on 1610 and 1510.
  975. * 730 has only 2 McBSP, and both of them are MPU peripherals.
  976. */
  977. static int __devinit omap_mcbsp_probe(struct platform_device *pdev)
  978. {
  979. struct omap_mcbsp_platform_data *pdata = pdev->dev.platform_data;
  980. struct omap_mcbsp *mcbsp;
  981. int id = pdev->id - 1;
  982. struct resource *res;
  983. int ret = 0;
  984. if (!pdata) {
  985. dev_err(&pdev->dev, "McBSP device initialized without"
  986. "platform data\n");
  987. ret = -EINVAL;
  988. goto exit;
  989. }
  990. dev_dbg(&pdev->dev, "Initializing OMAP McBSP (%d).\n", pdev->id);
  991. if (id >= omap_mcbsp_count) {
  992. dev_err(&pdev->dev, "Invalid McBSP device id (%d)\n", id);
  993. ret = -EINVAL;
  994. goto exit;
  995. }
  996. mcbsp = kzalloc(sizeof(struct omap_mcbsp), GFP_KERNEL);
  997. if (!mcbsp) {
  998. ret = -ENOMEM;
  999. goto exit;
  1000. }
  1001. spin_lock_init(&mcbsp->lock);
  1002. mcbsp->id = id + 1;
  1003. mcbsp->free = true;
  1004. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mpu");
  1005. if (!res) {
  1006. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1007. if (!res) {
  1008. dev_err(&pdev->dev, "%s:mcbsp%d has invalid memory"
  1009. "resource\n", __func__, pdev->id);
  1010. ret = -ENOMEM;
  1011. goto exit;
  1012. }
  1013. }
  1014. mcbsp->phys_base = res->start;
  1015. omap_mcbsp_cache_size = resource_size(res);
  1016. mcbsp->io_base = ioremap(res->start, resource_size(res));
  1017. if (!mcbsp->io_base) {
  1018. ret = -ENOMEM;
  1019. goto err_ioremap;
  1020. }
  1021. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dma");
  1022. if (!res)
  1023. mcbsp->phys_dma_base = mcbsp->phys_base;
  1024. else
  1025. mcbsp->phys_dma_base = res->start;
  1026. mcbsp->tx_irq = platform_get_irq_byname(pdev, "tx");
  1027. mcbsp->rx_irq = platform_get_irq_byname(pdev, "rx");
  1028. /* From OMAP4 there will be a single irq line */
  1029. if (mcbsp->tx_irq == -ENXIO)
  1030. mcbsp->tx_irq = platform_get_irq(pdev, 0);
  1031. res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
  1032. if (!res) {
  1033. dev_err(&pdev->dev, "%s:mcbsp%d has invalid rx DMA channel\n",
  1034. __func__, pdev->id);
  1035. ret = -ENODEV;
  1036. goto err_res;
  1037. }
  1038. mcbsp->dma_rx_sync = res->start;
  1039. res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
  1040. if (!res) {
  1041. dev_err(&pdev->dev, "%s:mcbsp%d has invalid tx DMA channel\n",
  1042. __func__, pdev->id);
  1043. ret = -ENODEV;
  1044. goto err_res;
  1045. }
  1046. mcbsp->dma_tx_sync = res->start;
  1047. mcbsp->fclk = clk_get(&pdev->dev, "fck");
  1048. if (IS_ERR(mcbsp->fclk)) {
  1049. ret = PTR_ERR(mcbsp->fclk);
  1050. dev_err(&pdev->dev, "unable to get fck: %d\n", ret);
  1051. goto err_res;
  1052. }
  1053. mcbsp->pdata = pdata;
  1054. mcbsp->dev = &pdev->dev;
  1055. mcbsp_ptr[id] = mcbsp;
  1056. platform_set_drvdata(pdev, mcbsp);
  1057. pm_runtime_enable(mcbsp->dev);
  1058. mcbsp->dma_op_mode = MCBSP_DMA_MODE_ELEMENT;
  1059. if (mcbsp->pdata->buffer_size) {
  1060. /*
  1061. * Initially configure the maximum thresholds to a safe value.
  1062. * The McBSP FIFO usage with these values should not go under
  1063. * 16 locations.
  1064. * If the whole FIFO without safety buffer is used, than there
  1065. * is a possibility that the DMA will be not able to push the
  1066. * new data on time, causing channel shifts in runtime.
  1067. */
  1068. mcbsp->max_tx_thres = max_thres(mcbsp) - 0x10;
  1069. mcbsp->max_rx_thres = max_thres(mcbsp) - 0x10;
  1070. ret = sysfs_create_group(&mcbsp->dev->kobj,
  1071. &additional_attr_group);
  1072. if (ret) {
  1073. dev_err(mcbsp->dev,
  1074. "Unable to create additional controls\n");
  1075. goto err_thres;
  1076. }
  1077. } else {
  1078. mcbsp->max_tx_thres = -EINVAL;
  1079. mcbsp->max_rx_thres = -EINVAL;
  1080. }
  1081. /* Initialize mcbsp properties for OMAP34XX if needed / applicable */
  1082. omap34xx_device_init(mcbsp);
  1083. return 0;
  1084. err_thres:
  1085. clk_put(mcbsp->fclk);
  1086. err_res:
  1087. iounmap(mcbsp->io_base);
  1088. err_ioremap:
  1089. kfree(mcbsp);
  1090. exit:
  1091. return ret;
  1092. }
  1093. static int __devexit omap_mcbsp_remove(struct platform_device *pdev)
  1094. {
  1095. struct omap_mcbsp *mcbsp = platform_get_drvdata(pdev);
  1096. platform_set_drvdata(pdev, NULL);
  1097. if (mcbsp) {
  1098. if (mcbsp->pdata && mcbsp->pdata->ops &&
  1099. mcbsp->pdata->ops->free)
  1100. mcbsp->pdata->ops->free(mcbsp->id);
  1101. if (mcbsp->pdata->buffer_size)
  1102. sysfs_remove_group(&mcbsp->dev->kobj,
  1103. &additional_attr_group);
  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. }