mcbsp.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. /*
  2. * sound/soc/omap/mcbsp.c
  3. *
  4. * Copyright (C) 2004 Nokia Corporation
  5. * Author: Samuel Ortiz <samuel.ortiz@nokia.com>
  6. *
  7. * Contact: Jarkko Nikula <jarkko.nikula@bitmer.com>
  8. * Peter Ujfalusi <peter.ujfalusi@ti.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. * Multichannel mode not supported.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/device.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/err.h>
  22. #include <linux/clk.h>
  23. #include <linux/delay.h>
  24. #include <linux/io.h>
  25. #include <linux/slab.h>
  26. #include <plat/mcbsp.h>
  27. #include "mcbsp.h"
  28. static void omap_mcbsp_write(struct omap_mcbsp *mcbsp, u16 reg, u32 val)
  29. {
  30. void __iomem *addr = mcbsp->io_base + reg * mcbsp->pdata->reg_step;
  31. if (mcbsp->pdata->reg_size == 2) {
  32. ((u16 *)mcbsp->reg_cache)[reg] = (u16)val;
  33. __raw_writew((u16)val, addr);
  34. } else {
  35. ((u32 *)mcbsp->reg_cache)[reg] = val;
  36. __raw_writel(val, addr);
  37. }
  38. }
  39. static int omap_mcbsp_read(struct omap_mcbsp *mcbsp, u16 reg, bool from_cache)
  40. {
  41. void __iomem *addr = mcbsp->io_base + reg * mcbsp->pdata->reg_step;
  42. if (mcbsp->pdata->reg_size == 2) {
  43. return !from_cache ? __raw_readw(addr) :
  44. ((u16 *)mcbsp->reg_cache)[reg];
  45. } else {
  46. return !from_cache ? __raw_readl(addr) :
  47. ((u32 *)mcbsp->reg_cache)[reg];
  48. }
  49. }
  50. static void omap_mcbsp_st_write(struct omap_mcbsp *mcbsp, u16 reg, u32 val)
  51. {
  52. __raw_writel(val, mcbsp->st_data->io_base_st + reg);
  53. }
  54. static int omap_mcbsp_st_read(struct omap_mcbsp *mcbsp, u16 reg)
  55. {
  56. return __raw_readl(mcbsp->st_data->io_base_st + reg);
  57. }
  58. #define MCBSP_READ(mcbsp, reg) \
  59. omap_mcbsp_read(mcbsp, OMAP_MCBSP_REG_##reg, 0)
  60. #define MCBSP_WRITE(mcbsp, reg, val) \
  61. omap_mcbsp_write(mcbsp, OMAP_MCBSP_REG_##reg, val)
  62. #define MCBSP_READ_CACHE(mcbsp, reg) \
  63. omap_mcbsp_read(mcbsp, OMAP_MCBSP_REG_##reg, 1)
  64. #define MCBSP_ST_READ(mcbsp, reg) \
  65. omap_mcbsp_st_read(mcbsp, OMAP_ST_REG_##reg)
  66. #define MCBSP_ST_WRITE(mcbsp, reg, val) \
  67. omap_mcbsp_st_write(mcbsp, OMAP_ST_REG_##reg, val)
  68. static void omap_mcbsp_dump_reg(struct omap_mcbsp *mcbsp)
  69. {
  70. dev_dbg(mcbsp->dev, "**** McBSP%d regs ****\n", mcbsp->id);
  71. dev_dbg(mcbsp->dev, "DRR2: 0x%04x\n",
  72. MCBSP_READ(mcbsp, DRR2));
  73. dev_dbg(mcbsp->dev, "DRR1: 0x%04x\n",
  74. MCBSP_READ(mcbsp, DRR1));
  75. dev_dbg(mcbsp->dev, "DXR2: 0x%04x\n",
  76. MCBSP_READ(mcbsp, DXR2));
  77. dev_dbg(mcbsp->dev, "DXR1: 0x%04x\n",
  78. MCBSP_READ(mcbsp, DXR1));
  79. dev_dbg(mcbsp->dev, "SPCR2: 0x%04x\n",
  80. MCBSP_READ(mcbsp, SPCR2));
  81. dev_dbg(mcbsp->dev, "SPCR1: 0x%04x\n",
  82. MCBSP_READ(mcbsp, SPCR1));
  83. dev_dbg(mcbsp->dev, "RCR2: 0x%04x\n",
  84. MCBSP_READ(mcbsp, RCR2));
  85. dev_dbg(mcbsp->dev, "RCR1: 0x%04x\n",
  86. MCBSP_READ(mcbsp, RCR1));
  87. dev_dbg(mcbsp->dev, "XCR2: 0x%04x\n",
  88. MCBSP_READ(mcbsp, XCR2));
  89. dev_dbg(mcbsp->dev, "XCR1: 0x%04x\n",
  90. MCBSP_READ(mcbsp, XCR1));
  91. dev_dbg(mcbsp->dev, "SRGR2: 0x%04x\n",
  92. MCBSP_READ(mcbsp, SRGR2));
  93. dev_dbg(mcbsp->dev, "SRGR1: 0x%04x\n",
  94. MCBSP_READ(mcbsp, SRGR1));
  95. dev_dbg(mcbsp->dev, "PCR0: 0x%04x\n",
  96. MCBSP_READ(mcbsp, PCR0));
  97. dev_dbg(mcbsp->dev, "***********************\n");
  98. }
  99. static irqreturn_t omap_mcbsp_tx_irq_handler(int irq, void *dev_id)
  100. {
  101. struct omap_mcbsp *mcbsp_tx = dev_id;
  102. u16 irqst_spcr2;
  103. irqst_spcr2 = MCBSP_READ(mcbsp_tx, SPCR2);
  104. dev_dbg(mcbsp_tx->dev, "TX IRQ callback : 0x%x\n", irqst_spcr2);
  105. if (irqst_spcr2 & XSYNC_ERR) {
  106. dev_err(mcbsp_tx->dev, "TX Frame Sync Error! : 0x%x\n",
  107. irqst_spcr2);
  108. /* Writing zero to XSYNC_ERR clears the IRQ */
  109. MCBSP_WRITE(mcbsp_tx, SPCR2, MCBSP_READ_CACHE(mcbsp_tx, SPCR2));
  110. }
  111. return IRQ_HANDLED;
  112. }
  113. static irqreturn_t omap_mcbsp_rx_irq_handler(int irq, void *dev_id)
  114. {
  115. struct omap_mcbsp *mcbsp_rx = dev_id;
  116. u16 irqst_spcr1;
  117. irqst_spcr1 = MCBSP_READ(mcbsp_rx, SPCR1);
  118. dev_dbg(mcbsp_rx->dev, "RX IRQ callback : 0x%x\n", irqst_spcr1);
  119. if (irqst_spcr1 & RSYNC_ERR) {
  120. dev_err(mcbsp_rx->dev, "RX Frame Sync Error! : 0x%x\n",
  121. irqst_spcr1);
  122. /* Writing zero to RSYNC_ERR clears the IRQ */
  123. MCBSP_WRITE(mcbsp_rx, SPCR1, MCBSP_READ_CACHE(mcbsp_rx, SPCR1));
  124. }
  125. return IRQ_HANDLED;
  126. }
  127. /*
  128. * omap_mcbsp_config simply write a config to the
  129. * appropriate McBSP.
  130. * You either call this function or set the McBSP registers
  131. * by yourself before calling omap_mcbsp_start().
  132. */
  133. void omap_mcbsp_config(struct omap_mcbsp *mcbsp,
  134. const struct omap_mcbsp_reg_cfg *config)
  135. {
  136. dev_dbg(mcbsp->dev, "Configuring McBSP%d phys_base: 0x%08lx\n",
  137. mcbsp->id, mcbsp->phys_base);
  138. /* We write the given config */
  139. MCBSP_WRITE(mcbsp, SPCR2, config->spcr2);
  140. MCBSP_WRITE(mcbsp, SPCR1, config->spcr1);
  141. MCBSP_WRITE(mcbsp, RCR2, config->rcr2);
  142. MCBSP_WRITE(mcbsp, RCR1, config->rcr1);
  143. MCBSP_WRITE(mcbsp, XCR2, config->xcr2);
  144. MCBSP_WRITE(mcbsp, XCR1, config->xcr1);
  145. MCBSP_WRITE(mcbsp, SRGR2, config->srgr2);
  146. MCBSP_WRITE(mcbsp, SRGR1, config->srgr1);
  147. MCBSP_WRITE(mcbsp, MCR2, config->mcr2);
  148. MCBSP_WRITE(mcbsp, MCR1, config->mcr1);
  149. MCBSP_WRITE(mcbsp, PCR0, config->pcr0);
  150. if (mcbsp->pdata->has_ccr) {
  151. MCBSP_WRITE(mcbsp, XCCR, config->xccr);
  152. MCBSP_WRITE(mcbsp, RCCR, config->rccr);
  153. }
  154. /* Enable wakeup behavior */
  155. if (mcbsp->pdata->has_wakeup)
  156. MCBSP_WRITE(mcbsp, WAKEUPEN, XRDYEN | RRDYEN);
  157. }
  158. /**
  159. * omap_mcbsp_dma_reg_params - returns the address of mcbsp data register
  160. * @id - mcbsp id
  161. * @stream - indicates the direction of data flow (rx or tx)
  162. *
  163. * Returns the address of mcbsp data transmit register or data receive register
  164. * to be used by DMA for transferring/receiving data based on the value of
  165. * @stream for the requested mcbsp given by @id
  166. */
  167. static int omap_mcbsp_dma_reg_params(struct omap_mcbsp *mcbsp,
  168. unsigned int stream)
  169. {
  170. int data_reg;
  171. if (mcbsp->pdata->reg_size == 2) {
  172. if (stream)
  173. data_reg = OMAP_MCBSP_REG_DRR1;
  174. else
  175. data_reg = OMAP_MCBSP_REG_DXR1;
  176. } else {
  177. if (stream)
  178. data_reg = OMAP_MCBSP_REG_DRR;
  179. else
  180. data_reg = OMAP_MCBSP_REG_DXR;
  181. }
  182. return mcbsp->phys_dma_base + data_reg * mcbsp->pdata->reg_step;
  183. }
  184. static void omap_st_on(struct omap_mcbsp *mcbsp)
  185. {
  186. unsigned int w;
  187. if (mcbsp->pdata->enable_st_clock)
  188. mcbsp->pdata->enable_st_clock(mcbsp->id, 1);
  189. /* Enable McBSP Sidetone */
  190. w = MCBSP_READ(mcbsp, SSELCR);
  191. MCBSP_WRITE(mcbsp, SSELCR, w | SIDETONEEN);
  192. /* Enable Sidetone from Sidetone Core */
  193. w = MCBSP_ST_READ(mcbsp, SSELCR);
  194. MCBSP_ST_WRITE(mcbsp, SSELCR, w | ST_SIDETONEEN);
  195. }
  196. static void omap_st_off(struct omap_mcbsp *mcbsp)
  197. {
  198. unsigned int w;
  199. w = MCBSP_ST_READ(mcbsp, SSELCR);
  200. MCBSP_ST_WRITE(mcbsp, SSELCR, w & ~(ST_SIDETONEEN));
  201. w = MCBSP_READ(mcbsp, SSELCR);
  202. MCBSP_WRITE(mcbsp, SSELCR, w & ~(SIDETONEEN));
  203. if (mcbsp->pdata->enable_st_clock)
  204. mcbsp->pdata->enable_st_clock(mcbsp->id, 0);
  205. }
  206. static void omap_st_fir_write(struct omap_mcbsp *mcbsp, s16 *fir)
  207. {
  208. u16 val, i;
  209. val = MCBSP_ST_READ(mcbsp, SSELCR);
  210. if (val & ST_COEFFWREN)
  211. MCBSP_ST_WRITE(mcbsp, SSELCR, val & ~(ST_COEFFWREN));
  212. MCBSP_ST_WRITE(mcbsp, SSELCR, val | ST_COEFFWREN);
  213. for (i = 0; i < 128; i++)
  214. MCBSP_ST_WRITE(mcbsp, SFIRCR, fir[i]);
  215. i = 0;
  216. val = MCBSP_ST_READ(mcbsp, SSELCR);
  217. while (!(val & ST_COEFFWRDONE) && (++i < 1000))
  218. val = MCBSP_ST_READ(mcbsp, SSELCR);
  219. MCBSP_ST_WRITE(mcbsp, SSELCR, val & ~(ST_COEFFWREN));
  220. if (i == 1000)
  221. dev_err(mcbsp->dev, "McBSP FIR load error!\n");
  222. }
  223. static void omap_st_chgain(struct omap_mcbsp *mcbsp)
  224. {
  225. u16 w;
  226. struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
  227. w = MCBSP_ST_READ(mcbsp, SSELCR);
  228. MCBSP_ST_WRITE(mcbsp, SGAINCR, ST_CH0GAIN(st_data->ch0gain) | \
  229. ST_CH1GAIN(st_data->ch1gain));
  230. }
  231. int omap_st_set_chgain(struct omap_mcbsp *mcbsp, int channel, s16 chgain)
  232. {
  233. struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
  234. int ret = 0;
  235. if (!st_data)
  236. return -ENOENT;
  237. spin_lock_irq(&mcbsp->lock);
  238. if (channel == 0)
  239. st_data->ch0gain = chgain;
  240. else if (channel == 1)
  241. st_data->ch1gain = chgain;
  242. else
  243. ret = -EINVAL;
  244. if (st_data->enabled)
  245. omap_st_chgain(mcbsp);
  246. spin_unlock_irq(&mcbsp->lock);
  247. return ret;
  248. }
  249. int omap_st_get_chgain(struct omap_mcbsp *mcbsp, int channel, s16 *chgain)
  250. {
  251. struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
  252. int ret = 0;
  253. if (!st_data)
  254. return -ENOENT;
  255. spin_lock_irq(&mcbsp->lock);
  256. if (channel == 0)
  257. *chgain = st_data->ch0gain;
  258. else if (channel == 1)
  259. *chgain = st_data->ch1gain;
  260. else
  261. ret = -EINVAL;
  262. spin_unlock_irq(&mcbsp->lock);
  263. return ret;
  264. }
  265. static int omap_st_start(struct omap_mcbsp *mcbsp)
  266. {
  267. struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
  268. if (st_data->enabled && !st_data->running) {
  269. omap_st_fir_write(mcbsp, st_data->taps);
  270. omap_st_chgain(mcbsp);
  271. if (!mcbsp->free) {
  272. omap_st_on(mcbsp);
  273. st_data->running = 1;
  274. }
  275. }
  276. return 0;
  277. }
  278. int omap_st_enable(struct omap_mcbsp *mcbsp)
  279. {
  280. struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
  281. if (!st_data)
  282. return -ENODEV;
  283. spin_lock_irq(&mcbsp->lock);
  284. st_data->enabled = 1;
  285. omap_st_start(mcbsp);
  286. spin_unlock_irq(&mcbsp->lock);
  287. return 0;
  288. }
  289. static int omap_st_stop(struct omap_mcbsp *mcbsp)
  290. {
  291. struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
  292. if (st_data->running) {
  293. if (!mcbsp->free) {
  294. omap_st_off(mcbsp);
  295. st_data->running = 0;
  296. }
  297. }
  298. return 0;
  299. }
  300. int omap_st_disable(struct omap_mcbsp *mcbsp)
  301. {
  302. struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
  303. int ret = 0;
  304. if (!st_data)
  305. return -ENODEV;
  306. spin_lock_irq(&mcbsp->lock);
  307. omap_st_stop(mcbsp);
  308. st_data->enabled = 0;
  309. spin_unlock_irq(&mcbsp->lock);
  310. return ret;
  311. }
  312. int omap_st_is_enabled(struct omap_mcbsp *mcbsp)
  313. {
  314. struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
  315. if (!st_data)
  316. return -ENODEV;
  317. return st_data->enabled;
  318. }
  319. /*
  320. * omap_mcbsp_set_rx_threshold configures the transmit threshold in words.
  321. * The threshold parameter is 1 based, and it is converted (threshold - 1)
  322. * for the THRSH2 register.
  323. */
  324. void omap_mcbsp_set_tx_threshold(struct omap_mcbsp *mcbsp, u16 threshold)
  325. {
  326. if (mcbsp->pdata->buffer_size == 0)
  327. return;
  328. if (threshold && threshold <= mcbsp->max_tx_thres)
  329. MCBSP_WRITE(mcbsp, THRSH2, threshold - 1);
  330. }
  331. /*
  332. * omap_mcbsp_set_rx_threshold configures the receive threshold in words.
  333. * The threshold parameter is 1 based, and it is converted (threshold - 1)
  334. * for the THRSH1 register.
  335. */
  336. void omap_mcbsp_set_rx_threshold(struct omap_mcbsp *mcbsp, u16 threshold)
  337. {
  338. if (mcbsp->pdata->buffer_size == 0)
  339. return;
  340. if (threshold && threshold <= mcbsp->max_rx_thres)
  341. MCBSP_WRITE(mcbsp, THRSH1, threshold - 1);
  342. }
  343. /*
  344. * omap_mcbsp_get_tx_delay returns the number of used slots in the McBSP FIFO
  345. */
  346. u16 omap_mcbsp_get_tx_delay(struct omap_mcbsp *mcbsp)
  347. {
  348. u16 buffstat;
  349. if (mcbsp->pdata->buffer_size == 0)
  350. return 0;
  351. /* Returns the number of free locations in the buffer */
  352. buffstat = MCBSP_READ(mcbsp, XBUFFSTAT);
  353. /* Number of slots are different in McBSP ports */
  354. return mcbsp->pdata->buffer_size - buffstat;
  355. }
  356. /*
  357. * omap_mcbsp_get_rx_delay returns the number of free slots in the McBSP FIFO
  358. * to reach the threshold value (when the DMA will be triggered to read it)
  359. */
  360. u16 omap_mcbsp_get_rx_delay(struct omap_mcbsp *mcbsp)
  361. {
  362. u16 buffstat, threshold;
  363. if (mcbsp->pdata->buffer_size == 0)
  364. return 0;
  365. /* Returns the number of used locations in the buffer */
  366. buffstat = MCBSP_READ(mcbsp, RBUFFSTAT);
  367. /* RX threshold */
  368. threshold = MCBSP_READ(mcbsp, THRSH1);
  369. /* Return the number of location till we reach the threshold limit */
  370. if (threshold <= buffstat)
  371. return 0;
  372. else
  373. return threshold - buffstat;
  374. }
  375. int omap_mcbsp_request(struct omap_mcbsp *mcbsp)
  376. {
  377. void *reg_cache;
  378. int err;
  379. reg_cache = kzalloc(mcbsp->reg_cache_size, GFP_KERNEL);
  380. if (!reg_cache) {
  381. return -ENOMEM;
  382. }
  383. spin_lock(&mcbsp->lock);
  384. if (!mcbsp->free) {
  385. dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
  386. mcbsp->id);
  387. err = -EBUSY;
  388. goto err_kfree;
  389. }
  390. mcbsp->free = false;
  391. mcbsp->reg_cache = reg_cache;
  392. spin_unlock(&mcbsp->lock);
  393. if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->request)
  394. mcbsp->pdata->ops->request(mcbsp->id - 1);
  395. /*
  396. * Make sure that transmitter, receiver and sample-rate generator are
  397. * not running before activating IRQs.
  398. */
  399. MCBSP_WRITE(mcbsp, SPCR1, 0);
  400. MCBSP_WRITE(mcbsp, SPCR2, 0);
  401. err = request_irq(mcbsp->tx_irq, omap_mcbsp_tx_irq_handler,
  402. 0, "McBSP", (void *)mcbsp);
  403. if (err != 0) {
  404. dev_err(mcbsp->dev, "Unable to request TX IRQ %d "
  405. "for McBSP%d\n", mcbsp->tx_irq,
  406. mcbsp->id);
  407. goto err_clk_disable;
  408. }
  409. if (mcbsp->rx_irq) {
  410. err = request_irq(mcbsp->rx_irq,
  411. omap_mcbsp_rx_irq_handler,
  412. 0, "McBSP", (void *)mcbsp);
  413. if (err != 0) {
  414. dev_err(mcbsp->dev, "Unable to request RX IRQ %d "
  415. "for McBSP%d\n", mcbsp->rx_irq,
  416. mcbsp->id);
  417. goto err_free_irq;
  418. }
  419. }
  420. return 0;
  421. err_free_irq:
  422. free_irq(mcbsp->tx_irq, (void *)mcbsp);
  423. err_clk_disable:
  424. if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
  425. mcbsp->pdata->ops->free(mcbsp->id - 1);
  426. /* Disable wakeup behavior */
  427. if (mcbsp->pdata->has_wakeup)
  428. MCBSP_WRITE(mcbsp, WAKEUPEN, 0);
  429. spin_lock(&mcbsp->lock);
  430. mcbsp->free = true;
  431. mcbsp->reg_cache = NULL;
  432. err_kfree:
  433. spin_unlock(&mcbsp->lock);
  434. kfree(reg_cache);
  435. return err;
  436. }
  437. void omap_mcbsp_free(struct omap_mcbsp *mcbsp)
  438. {
  439. void *reg_cache;
  440. if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
  441. mcbsp->pdata->ops->free(mcbsp->id - 1);
  442. /* Disable wakeup behavior */
  443. if (mcbsp->pdata->has_wakeup)
  444. MCBSP_WRITE(mcbsp, WAKEUPEN, 0);
  445. if (mcbsp->rx_irq)
  446. free_irq(mcbsp->rx_irq, (void *)mcbsp);
  447. free_irq(mcbsp->tx_irq, (void *)mcbsp);
  448. reg_cache = mcbsp->reg_cache;
  449. spin_lock(&mcbsp->lock);
  450. if (mcbsp->free)
  451. dev_err(mcbsp->dev, "McBSP%d was not reserved\n", mcbsp->id);
  452. else
  453. mcbsp->free = true;
  454. mcbsp->reg_cache = NULL;
  455. spin_unlock(&mcbsp->lock);
  456. if (reg_cache)
  457. kfree(reg_cache);
  458. }
  459. /*
  460. * Here we start the McBSP, by enabling transmitter, receiver or both.
  461. * If no transmitter or receiver is active prior calling, then sample-rate
  462. * generator and frame sync are started.
  463. */
  464. void omap_mcbsp_start(struct omap_mcbsp *mcbsp, int tx, int rx)
  465. {
  466. int enable_srg = 0;
  467. u16 w;
  468. if (mcbsp->st_data)
  469. omap_st_start(mcbsp);
  470. /* Only enable SRG, if McBSP is master */
  471. w = MCBSP_READ_CACHE(mcbsp, PCR0);
  472. if (w & (FSXM | FSRM | CLKXM | CLKRM))
  473. enable_srg = !((MCBSP_READ_CACHE(mcbsp, SPCR2) |
  474. MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1);
  475. if (enable_srg) {
  476. /* Start the sample generator */
  477. w = MCBSP_READ_CACHE(mcbsp, SPCR2);
  478. MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 6));
  479. }
  480. /* Enable transmitter and receiver */
  481. tx &= 1;
  482. w = MCBSP_READ_CACHE(mcbsp, SPCR2);
  483. MCBSP_WRITE(mcbsp, SPCR2, w | tx);
  484. rx &= 1;
  485. w = MCBSP_READ_CACHE(mcbsp, SPCR1);
  486. MCBSP_WRITE(mcbsp, SPCR1, w | rx);
  487. /*
  488. * Worst case: CLKSRG*2 = 8000khz: (1/8000) * 2 * 2 usec
  489. * REVISIT: 100us may give enough time for two CLKSRG, however
  490. * due to some unknown PM related, clock gating etc. reason it
  491. * is now at 500us.
  492. */
  493. udelay(500);
  494. if (enable_srg) {
  495. /* Start frame sync */
  496. w = MCBSP_READ_CACHE(mcbsp, SPCR2);
  497. MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 7));
  498. }
  499. if (mcbsp->pdata->has_ccr) {
  500. /* Release the transmitter and receiver */
  501. w = MCBSP_READ_CACHE(mcbsp, XCCR);
  502. w &= ~(tx ? XDISABLE : 0);
  503. MCBSP_WRITE(mcbsp, XCCR, w);
  504. w = MCBSP_READ_CACHE(mcbsp, RCCR);
  505. w &= ~(rx ? RDISABLE : 0);
  506. MCBSP_WRITE(mcbsp, RCCR, w);
  507. }
  508. /* Dump McBSP Regs */
  509. omap_mcbsp_dump_reg(mcbsp);
  510. }
  511. void omap_mcbsp_stop(struct omap_mcbsp *mcbsp, int tx, int rx)
  512. {
  513. int idle;
  514. u16 w;
  515. /* Reset transmitter */
  516. tx &= 1;
  517. if (mcbsp->pdata->has_ccr) {
  518. w = MCBSP_READ_CACHE(mcbsp, XCCR);
  519. w |= (tx ? XDISABLE : 0);
  520. MCBSP_WRITE(mcbsp, XCCR, w);
  521. }
  522. w = MCBSP_READ_CACHE(mcbsp, SPCR2);
  523. MCBSP_WRITE(mcbsp, SPCR2, w & ~tx);
  524. /* Reset receiver */
  525. rx &= 1;
  526. if (mcbsp->pdata->has_ccr) {
  527. w = MCBSP_READ_CACHE(mcbsp, RCCR);
  528. w |= (rx ? RDISABLE : 0);
  529. MCBSP_WRITE(mcbsp, RCCR, w);
  530. }
  531. w = MCBSP_READ_CACHE(mcbsp, SPCR1);
  532. MCBSP_WRITE(mcbsp, SPCR1, w & ~rx);
  533. idle = !((MCBSP_READ_CACHE(mcbsp, SPCR2) |
  534. MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1);
  535. if (idle) {
  536. /* Reset the sample rate generator */
  537. w = MCBSP_READ_CACHE(mcbsp, SPCR2);
  538. MCBSP_WRITE(mcbsp, SPCR2, w & ~(1 << 6));
  539. }
  540. if (mcbsp->st_data)
  541. omap_st_stop(mcbsp);
  542. }
  543. int omap2_mcbsp_set_clks_src(struct omap_mcbsp *mcbsp, u8 fck_src_id)
  544. {
  545. const char *src;
  546. if (fck_src_id == MCBSP_CLKS_PAD_SRC)
  547. src = "clks_ext";
  548. else if (fck_src_id == MCBSP_CLKS_PRCM_SRC)
  549. src = "clks_fclk";
  550. else
  551. return -EINVAL;
  552. if (mcbsp->pdata->set_clk_src)
  553. return mcbsp->pdata->set_clk_src(mcbsp->dev, mcbsp->fclk, src);
  554. else
  555. return -EINVAL;
  556. }
  557. void omap2_mcbsp1_mux_clkr_src(struct omap_mcbsp *mcbsp, u8 mux)
  558. {
  559. const char *src;
  560. if (mcbsp->id != 1)
  561. return;
  562. if (mux == CLKR_SRC_CLKR)
  563. src = "clkr";
  564. else if (mux == CLKR_SRC_CLKX)
  565. src = "clkx";
  566. else
  567. return;
  568. if (mcbsp->pdata->mux_signal)
  569. mcbsp->pdata->mux_signal(mcbsp->dev, "clkr", src);
  570. }
  571. void omap2_mcbsp1_mux_fsr_src(struct omap_mcbsp *mcbsp, u8 mux)
  572. {
  573. const char *src;
  574. if (mcbsp->id != 1)
  575. return;
  576. if (mux == FSR_SRC_FSR)
  577. src = "fsr";
  578. else if (mux == FSR_SRC_FSX)
  579. src = "fsx";
  580. else
  581. return;
  582. if (mcbsp->pdata->mux_signal)
  583. mcbsp->pdata->mux_signal(mcbsp->dev, "fsr", src);
  584. }
  585. #define max_thres(m) (mcbsp->pdata->buffer_size)
  586. #define valid_threshold(m, val) ((val) <= max_thres(m))
  587. #define THRESHOLD_PROP_BUILDER(prop) \
  588. static ssize_t prop##_show(struct device *dev, \
  589. struct device_attribute *attr, char *buf) \
  590. { \
  591. struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
  592. \
  593. return sprintf(buf, "%u\n", mcbsp->prop); \
  594. } \
  595. \
  596. static ssize_t prop##_store(struct device *dev, \
  597. struct device_attribute *attr, \
  598. const char *buf, size_t size) \
  599. { \
  600. struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
  601. unsigned long val; \
  602. int status; \
  603. \
  604. status = strict_strtoul(buf, 0, &val); \
  605. if (status) \
  606. return status; \
  607. \
  608. if (!valid_threshold(mcbsp, val)) \
  609. return -EDOM; \
  610. \
  611. mcbsp->prop = val; \
  612. return size; \
  613. } \
  614. \
  615. static DEVICE_ATTR(prop, 0644, prop##_show, prop##_store);
  616. THRESHOLD_PROP_BUILDER(max_tx_thres);
  617. THRESHOLD_PROP_BUILDER(max_rx_thres);
  618. static const char *dma_op_modes[] = {
  619. "element", "threshold", "frame",
  620. };
  621. static ssize_t dma_op_mode_show(struct device *dev,
  622. struct device_attribute *attr, char *buf)
  623. {
  624. struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
  625. int dma_op_mode, i = 0;
  626. ssize_t len = 0;
  627. const char * const *s;
  628. dma_op_mode = mcbsp->dma_op_mode;
  629. for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++) {
  630. if (dma_op_mode == i)
  631. len += sprintf(buf + len, "[%s] ", *s);
  632. else
  633. len += sprintf(buf + len, "%s ", *s);
  634. }
  635. len += sprintf(buf + len, "\n");
  636. return len;
  637. }
  638. static ssize_t dma_op_mode_store(struct device *dev,
  639. struct device_attribute *attr,
  640. const char *buf, size_t size)
  641. {
  642. struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
  643. const char * const *s;
  644. int i = 0;
  645. for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++)
  646. if (sysfs_streq(buf, *s))
  647. break;
  648. if (i == ARRAY_SIZE(dma_op_modes))
  649. return -EINVAL;
  650. spin_lock_irq(&mcbsp->lock);
  651. if (!mcbsp->free) {
  652. size = -EBUSY;
  653. goto unlock;
  654. }
  655. mcbsp->dma_op_mode = i;
  656. unlock:
  657. spin_unlock_irq(&mcbsp->lock);
  658. return size;
  659. }
  660. static DEVICE_ATTR(dma_op_mode, 0644, dma_op_mode_show, dma_op_mode_store);
  661. static const struct attribute *additional_attrs[] = {
  662. &dev_attr_max_tx_thres.attr,
  663. &dev_attr_max_rx_thres.attr,
  664. &dev_attr_dma_op_mode.attr,
  665. NULL,
  666. };
  667. static const struct attribute_group additional_attr_group = {
  668. .attrs = (struct attribute **)additional_attrs,
  669. };
  670. static ssize_t st_taps_show(struct device *dev,
  671. struct device_attribute *attr, char *buf)
  672. {
  673. struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
  674. struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
  675. ssize_t status = 0;
  676. int i;
  677. spin_lock_irq(&mcbsp->lock);
  678. for (i = 0; i < st_data->nr_taps; i++)
  679. status += sprintf(&buf[status], (i ? ", %d" : "%d"),
  680. st_data->taps[i]);
  681. if (i)
  682. status += sprintf(&buf[status], "\n");
  683. spin_unlock_irq(&mcbsp->lock);
  684. return status;
  685. }
  686. static ssize_t st_taps_store(struct device *dev,
  687. struct device_attribute *attr,
  688. const char *buf, size_t size)
  689. {
  690. struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
  691. struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
  692. int val, tmp, status, i = 0;
  693. spin_lock_irq(&mcbsp->lock);
  694. memset(st_data->taps, 0, sizeof(st_data->taps));
  695. st_data->nr_taps = 0;
  696. do {
  697. status = sscanf(buf, "%d%n", &val, &tmp);
  698. if (status < 0 || status == 0) {
  699. size = -EINVAL;
  700. goto out;
  701. }
  702. if (val < -32768 || val > 32767) {
  703. size = -EINVAL;
  704. goto out;
  705. }
  706. st_data->taps[i++] = val;
  707. buf += tmp;
  708. if (*buf != ',')
  709. break;
  710. buf++;
  711. } while (1);
  712. st_data->nr_taps = i;
  713. out:
  714. spin_unlock_irq(&mcbsp->lock);
  715. return size;
  716. }
  717. static DEVICE_ATTR(st_taps, 0644, st_taps_show, st_taps_store);
  718. static const struct attribute *sidetone_attrs[] = {
  719. &dev_attr_st_taps.attr,
  720. NULL,
  721. };
  722. static const struct attribute_group sidetone_attr_group = {
  723. .attrs = (struct attribute **)sidetone_attrs,
  724. };
  725. static int __devinit omap_st_add(struct omap_mcbsp *mcbsp,
  726. struct resource *res)
  727. {
  728. struct omap_mcbsp_st_data *st_data;
  729. int err;
  730. st_data = devm_kzalloc(mcbsp->dev, sizeof(*mcbsp->st_data), GFP_KERNEL);
  731. if (!st_data)
  732. return -ENOMEM;
  733. st_data->io_base_st = devm_ioremap(mcbsp->dev, res->start,
  734. resource_size(res));
  735. if (!st_data->io_base_st)
  736. return -ENOMEM;
  737. err = sysfs_create_group(&mcbsp->dev->kobj, &sidetone_attr_group);
  738. if (err)
  739. return err;
  740. mcbsp->st_data = st_data;
  741. return 0;
  742. }
  743. /*
  744. * McBSP1 and McBSP3 are directly mapped on 1610 and 1510.
  745. * 730 has only 2 McBSP, and both of them are MPU peripherals.
  746. */
  747. int __devinit omap_mcbsp_init(struct platform_device *pdev)
  748. {
  749. struct omap_mcbsp *mcbsp = platform_get_drvdata(pdev);
  750. struct resource *res;
  751. int ret = 0;
  752. spin_lock_init(&mcbsp->lock);
  753. mcbsp->free = true;
  754. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mpu");
  755. if (!res) {
  756. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  757. if (!res) {
  758. dev_err(mcbsp->dev, "invalid memory resource\n");
  759. return -ENOMEM;
  760. }
  761. }
  762. if (!devm_request_mem_region(&pdev->dev, res->start, resource_size(res),
  763. dev_name(&pdev->dev))) {
  764. dev_err(mcbsp->dev, "memory region already claimed\n");
  765. return -ENODEV;
  766. }
  767. mcbsp->phys_base = res->start;
  768. mcbsp->reg_cache_size = resource_size(res);
  769. mcbsp->io_base = devm_ioremap(&pdev->dev, res->start,
  770. resource_size(res));
  771. if (!mcbsp->io_base)
  772. return -ENOMEM;
  773. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dma");
  774. if (!res)
  775. mcbsp->phys_dma_base = mcbsp->phys_base;
  776. else
  777. mcbsp->phys_dma_base = res->start;
  778. mcbsp->tx_irq = platform_get_irq_byname(pdev, "tx");
  779. mcbsp->rx_irq = platform_get_irq_byname(pdev, "rx");
  780. /* From OMAP4 there will be a single irq line */
  781. if (mcbsp->tx_irq == -ENXIO)
  782. mcbsp->tx_irq = platform_get_irq(pdev, 0);
  783. res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
  784. if (!res) {
  785. dev_err(&pdev->dev, "invalid rx DMA channel\n");
  786. return -ENODEV;
  787. }
  788. /* RX DMA request number, and port address configuration */
  789. mcbsp->dma_data[1].name = "Audio Capture";
  790. mcbsp->dma_data[1].dma_req = res->start;
  791. mcbsp->dma_data[1].port_addr = omap_mcbsp_dma_reg_params(mcbsp, 1);
  792. res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
  793. if (!res) {
  794. dev_err(&pdev->dev, "invalid tx DMA channel\n");
  795. return -ENODEV;
  796. }
  797. /* TX DMA request number, and port address configuration */
  798. mcbsp->dma_data[0].name = "Audio Playback";
  799. mcbsp->dma_data[0].dma_req = res->start;
  800. mcbsp->dma_data[0].port_addr = omap_mcbsp_dma_reg_params(mcbsp, 0);
  801. mcbsp->fclk = clk_get(&pdev->dev, "fck");
  802. if (IS_ERR(mcbsp->fclk)) {
  803. ret = PTR_ERR(mcbsp->fclk);
  804. dev_err(mcbsp->dev, "unable to get fck: %d\n", ret);
  805. return ret;
  806. }
  807. mcbsp->dma_op_mode = MCBSP_DMA_MODE_ELEMENT;
  808. if (mcbsp->pdata->buffer_size) {
  809. /*
  810. * Initially configure the maximum thresholds to a safe value.
  811. * The McBSP FIFO usage with these values should not go under
  812. * 16 locations.
  813. * If the whole FIFO without safety buffer is used, than there
  814. * is a possibility that the DMA will be not able to push the
  815. * new data on time, causing channel shifts in runtime.
  816. */
  817. mcbsp->max_tx_thres = max_thres(mcbsp) - 0x10;
  818. mcbsp->max_rx_thres = max_thres(mcbsp) - 0x10;
  819. ret = sysfs_create_group(&mcbsp->dev->kobj,
  820. &additional_attr_group);
  821. if (ret) {
  822. dev_err(mcbsp->dev,
  823. "Unable to create additional controls\n");
  824. goto err_thres;
  825. }
  826. } else {
  827. mcbsp->max_tx_thres = -EINVAL;
  828. mcbsp->max_rx_thres = -EINVAL;
  829. }
  830. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sidetone");
  831. if (res) {
  832. ret = omap_st_add(mcbsp, res);
  833. if (ret) {
  834. dev_err(mcbsp->dev,
  835. "Unable to create sidetone controls\n");
  836. goto err_st;
  837. }
  838. }
  839. return 0;
  840. err_st:
  841. if (mcbsp->pdata->buffer_size)
  842. sysfs_remove_group(&mcbsp->dev->kobj, &additional_attr_group);
  843. err_thres:
  844. clk_put(mcbsp->fclk);
  845. return ret;
  846. }
  847. void __devexit omap_mcbsp_sysfs_remove(struct omap_mcbsp *mcbsp)
  848. {
  849. if (mcbsp->pdata->buffer_size)
  850. sysfs_remove_group(&mcbsp->dev->kobj, &additional_attr_group);
  851. if (mcbsp->st_data)
  852. sysfs_remove_group(&mcbsp->dev->kobj, &sidetone_attr_group);
  853. }