mcbsp.c 27 KB

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