mcbsp.c 33 KB

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