mcbsp.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899
  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/wait.h>
  19. #include <linux/completion.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/dma.h>
  27. #include <plat/mcbsp.h>
  28. #include <plat/omap_device.h>
  29. /* XXX These "sideways" includes are a sign that something is wrong */
  30. #include "../mach-omap2/cm2xxx_3xxx.h"
  31. #include "../mach-omap2/cm-regbits-34xx.h"
  32. struct omap_mcbsp **mcbsp_ptr;
  33. int omap_mcbsp_count, omap_mcbsp_cache_size;
  34. static void omap_mcbsp_write(struct omap_mcbsp *mcbsp, u16 reg, u32 val)
  35. {
  36. if (cpu_class_is_omap1()) {
  37. ((u16 *)mcbsp->reg_cache)[reg / sizeof(u16)] = (u16)val;
  38. __raw_writew((u16)val, mcbsp->io_base + reg);
  39. } else if (cpu_is_omap2420()) {
  40. ((u16 *)mcbsp->reg_cache)[reg / sizeof(u32)] = (u16)val;
  41. __raw_writew((u16)val, mcbsp->io_base + reg);
  42. } else {
  43. ((u32 *)mcbsp->reg_cache)[reg / sizeof(u32)] = val;
  44. __raw_writel(val, mcbsp->io_base + reg);
  45. }
  46. }
  47. static int omap_mcbsp_read(struct omap_mcbsp *mcbsp, u16 reg, bool from_cache)
  48. {
  49. if (cpu_class_is_omap1()) {
  50. return !from_cache ? __raw_readw(mcbsp->io_base + reg) :
  51. ((u16 *)mcbsp->reg_cache)[reg / sizeof(u16)];
  52. } else if (cpu_is_omap2420()) {
  53. return !from_cache ? __raw_readw(mcbsp->io_base + reg) :
  54. ((u16 *)mcbsp->reg_cache)[reg / sizeof(u32)];
  55. } else {
  56. return !from_cache ? __raw_readl(mcbsp->io_base + reg) :
  57. ((u32 *)mcbsp->reg_cache)[reg / sizeof(u32)];
  58. }
  59. }
  60. #ifdef CONFIG_ARCH_OMAP3
  61. static void omap_mcbsp_st_write(struct omap_mcbsp *mcbsp, u16 reg, u32 val)
  62. {
  63. __raw_writel(val, mcbsp->st_data->io_base_st + reg);
  64. }
  65. static int omap_mcbsp_st_read(struct omap_mcbsp *mcbsp, u16 reg)
  66. {
  67. return __raw_readl(mcbsp->st_data->io_base_st + reg);
  68. }
  69. #endif
  70. #define MCBSP_READ(mcbsp, reg) \
  71. omap_mcbsp_read(mcbsp, OMAP_MCBSP_REG_##reg, 0)
  72. #define MCBSP_WRITE(mcbsp, reg, val) \
  73. omap_mcbsp_write(mcbsp, OMAP_MCBSP_REG_##reg, val)
  74. #define MCBSP_READ_CACHE(mcbsp, reg) \
  75. omap_mcbsp_read(mcbsp, OMAP_MCBSP_REG_##reg, 1)
  76. #define MCBSP_ST_READ(mcbsp, reg) \
  77. omap_mcbsp_st_read(mcbsp, OMAP_ST_REG_##reg)
  78. #define MCBSP_ST_WRITE(mcbsp, reg, val) \
  79. omap_mcbsp_st_write(mcbsp, OMAP_ST_REG_##reg, val)
  80. static void omap_mcbsp_dump_reg(u8 id)
  81. {
  82. struct omap_mcbsp *mcbsp = id_to_mcbsp_ptr(id);
  83. dev_dbg(mcbsp->dev, "**** McBSP%d regs ****\n", mcbsp->id);
  84. dev_dbg(mcbsp->dev, "DRR2: 0x%04x\n",
  85. MCBSP_READ(mcbsp, DRR2));
  86. dev_dbg(mcbsp->dev, "DRR1: 0x%04x\n",
  87. MCBSP_READ(mcbsp, DRR1));
  88. dev_dbg(mcbsp->dev, "DXR2: 0x%04x\n",
  89. MCBSP_READ(mcbsp, DXR2));
  90. dev_dbg(mcbsp->dev, "DXR1: 0x%04x\n",
  91. MCBSP_READ(mcbsp, DXR1));
  92. dev_dbg(mcbsp->dev, "SPCR2: 0x%04x\n",
  93. MCBSP_READ(mcbsp, SPCR2));
  94. dev_dbg(mcbsp->dev, "SPCR1: 0x%04x\n",
  95. MCBSP_READ(mcbsp, SPCR1));
  96. dev_dbg(mcbsp->dev, "RCR2: 0x%04x\n",
  97. MCBSP_READ(mcbsp, RCR2));
  98. dev_dbg(mcbsp->dev, "RCR1: 0x%04x\n",
  99. MCBSP_READ(mcbsp, RCR1));
  100. dev_dbg(mcbsp->dev, "XCR2: 0x%04x\n",
  101. MCBSP_READ(mcbsp, XCR2));
  102. dev_dbg(mcbsp->dev, "XCR1: 0x%04x\n",
  103. MCBSP_READ(mcbsp, XCR1));
  104. dev_dbg(mcbsp->dev, "SRGR2: 0x%04x\n",
  105. MCBSP_READ(mcbsp, SRGR2));
  106. dev_dbg(mcbsp->dev, "SRGR1: 0x%04x\n",
  107. MCBSP_READ(mcbsp, SRGR1));
  108. dev_dbg(mcbsp->dev, "PCR0: 0x%04x\n",
  109. MCBSP_READ(mcbsp, PCR0));
  110. dev_dbg(mcbsp->dev, "***********************\n");
  111. }
  112. static irqreturn_t omap_mcbsp_tx_irq_handler(int irq, void *dev_id)
  113. {
  114. struct omap_mcbsp *mcbsp_tx = dev_id;
  115. u16 irqst_spcr2;
  116. irqst_spcr2 = MCBSP_READ(mcbsp_tx, SPCR2);
  117. dev_dbg(mcbsp_tx->dev, "TX IRQ callback : 0x%x\n", irqst_spcr2);
  118. if (irqst_spcr2 & XSYNC_ERR) {
  119. dev_err(mcbsp_tx->dev, "TX Frame Sync Error! : 0x%x\n",
  120. irqst_spcr2);
  121. /* Writing zero to XSYNC_ERR clears the IRQ */
  122. MCBSP_WRITE(mcbsp_tx, SPCR2, MCBSP_READ_CACHE(mcbsp_tx, SPCR2));
  123. } else {
  124. complete(&mcbsp_tx->tx_irq_completion);
  125. }
  126. return IRQ_HANDLED;
  127. }
  128. static irqreturn_t omap_mcbsp_rx_irq_handler(int irq, void *dev_id)
  129. {
  130. struct omap_mcbsp *mcbsp_rx = dev_id;
  131. u16 irqst_spcr1;
  132. irqst_spcr1 = MCBSP_READ(mcbsp_rx, SPCR1);
  133. dev_dbg(mcbsp_rx->dev, "RX IRQ callback : 0x%x\n", irqst_spcr1);
  134. if (irqst_spcr1 & RSYNC_ERR) {
  135. dev_err(mcbsp_rx->dev, "RX Frame Sync Error! : 0x%x\n",
  136. irqst_spcr1);
  137. /* Writing zero to RSYNC_ERR clears the IRQ */
  138. MCBSP_WRITE(mcbsp_rx, SPCR1, MCBSP_READ_CACHE(mcbsp_rx, SPCR1));
  139. } else {
  140. complete(&mcbsp_rx->rx_irq_completion);
  141. }
  142. return IRQ_HANDLED;
  143. }
  144. static void omap_mcbsp_tx_dma_callback(int lch, u16 ch_status, void *data)
  145. {
  146. struct omap_mcbsp *mcbsp_dma_tx = data;
  147. dev_dbg(mcbsp_dma_tx->dev, "TX DMA callback : 0x%x\n",
  148. MCBSP_READ(mcbsp_dma_tx, SPCR2));
  149. /* We can free the channels */
  150. omap_free_dma(mcbsp_dma_tx->dma_tx_lch);
  151. mcbsp_dma_tx->dma_tx_lch = -1;
  152. complete(&mcbsp_dma_tx->tx_dma_completion);
  153. }
  154. static void omap_mcbsp_rx_dma_callback(int lch, u16 ch_status, void *data)
  155. {
  156. struct omap_mcbsp *mcbsp_dma_rx = data;
  157. dev_dbg(mcbsp_dma_rx->dev, "RX DMA callback : 0x%x\n",
  158. MCBSP_READ(mcbsp_dma_rx, SPCR2));
  159. /* We can free the channels */
  160. omap_free_dma(mcbsp_dma_rx->dma_rx_lch);
  161. mcbsp_dma_rx->dma_rx_lch = -1;
  162. complete(&mcbsp_dma_rx->rx_dma_completion);
  163. }
  164. /*
  165. * omap_mcbsp_config simply write a config to the
  166. * appropriate McBSP.
  167. * You either call this function or set the McBSP registers
  168. * by yourself before calling omap_mcbsp_start().
  169. */
  170. void omap_mcbsp_config(unsigned int id, const struct omap_mcbsp_reg_cfg *config)
  171. {
  172. struct omap_mcbsp *mcbsp;
  173. if (!omap_mcbsp_check_valid_id(id)) {
  174. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  175. return;
  176. }
  177. mcbsp = id_to_mcbsp_ptr(id);
  178. dev_dbg(mcbsp->dev, "Configuring McBSP%d phys_base: 0x%08lx\n",
  179. mcbsp->id, mcbsp->phys_base);
  180. /* We write the given config */
  181. MCBSP_WRITE(mcbsp, SPCR2, config->spcr2);
  182. MCBSP_WRITE(mcbsp, SPCR1, config->spcr1);
  183. MCBSP_WRITE(mcbsp, RCR2, config->rcr2);
  184. MCBSP_WRITE(mcbsp, RCR1, config->rcr1);
  185. MCBSP_WRITE(mcbsp, XCR2, config->xcr2);
  186. MCBSP_WRITE(mcbsp, XCR1, config->xcr1);
  187. MCBSP_WRITE(mcbsp, SRGR2, config->srgr2);
  188. MCBSP_WRITE(mcbsp, SRGR1, config->srgr1);
  189. MCBSP_WRITE(mcbsp, MCR2, config->mcr2);
  190. MCBSP_WRITE(mcbsp, MCR1, config->mcr1);
  191. MCBSP_WRITE(mcbsp, PCR0, config->pcr0);
  192. if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
  193. MCBSP_WRITE(mcbsp, XCCR, config->xccr);
  194. MCBSP_WRITE(mcbsp, RCCR, config->rccr);
  195. }
  196. }
  197. EXPORT_SYMBOL(omap_mcbsp_config);
  198. #ifdef CONFIG_ARCH_OMAP3
  199. static struct omap_device *find_omap_device_by_dev(struct device *dev)
  200. {
  201. struct platform_device *pdev = container_of(dev,
  202. struct platform_device, dev);
  203. return container_of(pdev, struct omap_device, pdev);
  204. }
  205. static void omap_st_on(struct omap_mcbsp *mcbsp)
  206. {
  207. unsigned int w;
  208. struct omap_device *od;
  209. od = find_omap_device_by_dev(mcbsp->dev);
  210. /*
  211. * Sidetone uses McBSP ICLK - which must not idle when sidetones
  212. * are enabled or sidetones start sounding ugly.
  213. */
  214. w = omap2_cm_read_mod_reg(OMAP3430_PER_MOD, CM_AUTOIDLE);
  215. w &= ~(1 << (mcbsp->id - 2));
  216. omap2_cm_write_mod_reg(w, OMAP3430_PER_MOD, CM_AUTOIDLE);
  217. /* Enable McBSP Sidetone */
  218. w = MCBSP_READ(mcbsp, SSELCR);
  219. MCBSP_WRITE(mcbsp, SSELCR, w | SIDETONEEN);
  220. /* Enable Sidetone from Sidetone Core */
  221. w = MCBSP_ST_READ(mcbsp, SSELCR);
  222. MCBSP_ST_WRITE(mcbsp, SSELCR, w | ST_SIDETONEEN);
  223. }
  224. static void omap_st_off(struct omap_mcbsp *mcbsp)
  225. {
  226. unsigned int w;
  227. struct omap_device *od;
  228. od = find_omap_device_by_dev(mcbsp->dev);
  229. w = MCBSP_ST_READ(mcbsp, SSELCR);
  230. MCBSP_ST_WRITE(mcbsp, SSELCR, w & ~(ST_SIDETONEEN));
  231. w = MCBSP_READ(mcbsp, SSELCR);
  232. MCBSP_WRITE(mcbsp, SSELCR, w & ~(SIDETONEEN));
  233. w = omap2_cm_read_mod_reg(OMAP3430_PER_MOD, CM_AUTOIDLE);
  234. w |= 1 << (mcbsp->id - 2);
  235. omap2_cm_write_mod_reg(w, OMAP3430_PER_MOD, CM_AUTOIDLE);
  236. }
  237. static void omap_st_fir_write(struct omap_mcbsp *mcbsp, s16 *fir)
  238. {
  239. u16 val, i;
  240. struct omap_device *od;
  241. od = find_omap_device_by_dev(mcbsp->dev);
  242. val = MCBSP_ST_READ(mcbsp, SSELCR);
  243. if (val & ST_COEFFWREN)
  244. MCBSP_ST_WRITE(mcbsp, SSELCR, val & ~(ST_COEFFWREN));
  245. MCBSP_ST_WRITE(mcbsp, SSELCR, val | ST_COEFFWREN);
  246. for (i = 0; i < 128; i++)
  247. MCBSP_ST_WRITE(mcbsp, SFIRCR, fir[i]);
  248. i = 0;
  249. val = MCBSP_ST_READ(mcbsp, SSELCR);
  250. while (!(val & ST_COEFFWRDONE) && (++i < 1000))
  251. val = MCBSP_ST_READ(mcbsp, SSELCR);
  252. MCBSP_ST_WRITE(mcbsp, SSELCR, val & ~(ST_COEFFWREN));
  253. if (i == 1000)
  254. dev_err(mcbsp->dev, "McBSP FIR load error!\n");
  255. }
  256. static void omap_st_chgain(struct omap_mcbsp *mcbsp)
  257. {
  258. u16 w;
  259. struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
  260. struct omap_device *od;
  261. od = find_omap_device_by_dev(mcbsp->dev);
  262. w = MCBSP_ST_READ(mcbsp, SSELCR);
  263. MCBSP_ST_WRITE(mcbsp, SGAINCR, ST_CH0GAIN(st_data->ch0gain) | \
  264. ST_CH1GAIN(st_data->ch1gain));
  265. }
  266. int omap_st_set_chgain(unsigned int id, int channel, s16 chgain)
  267. {
  268. struct omap_mcbsp *mcbsp;
  269. struct omap_mcbsp_st_data *st_data;
  270. int ret = 0;
  271. if (!omap_mcbsp_check_valid_id(id)) {
  272. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  273. return -ENODEV;
  274. }
  275. mcbsp = id_to_mcbsp_ptr(id);
  276. st_data = mcbsp->st_data;
  277. if (!st_data)
  278. return -ENOENT;
  279. spin_lock_irq(&mcbsp->lock);
  280. if (channel == 0)
  281. st_data->ch0gain = chgain;
  282. else if (channel == 1)
  283. st_data->ch1gain = chgain;
  284. else
  285. ret = -EINVAL;
  286. if (st_data->enabled)
  287. omap_st_chgain(mcbsp);
  288. spin_unlock_irq(&mcbsp->lock);
  289. return ret;
  290. }
  291. EXPORT_SYMBOL(omap_st_set_chgain);
  292. int omap_st_get_chgain(unsigned int id, int channel, s16 *chgain)
  293. {
  294. struct omap_mcbsp *mcbsp;
  295. struct omap_mcbsp_st_data *st_data;
  296. int ret = 0;
  297. if (!omap_mcbsp_check_valid_id(id)) {
  298. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  299. return -ENODEV;
  300. }
  301. mcbsp = id_to_mcbsp_ptr(id);
  302. st_data = mcbsp->st_data;
  303. if (!st_data)
  304. return -ENOENT;
  305. spin_lock_irq(&mcbsp->lock);
  306. if (channel == 0)
  307. *chgain = st_data->ch0gain;
  308. else if (channel == 1)
  309. *chgain = st_data->ch1gain;
  310. else
  311. ret = -EINVAL;
  312. spin_unlock_irq(&mcbsp->lock);
  313. return ret;
  314. }
  315. EXPORT_SYMBOL(omap_st_get_chgain);
  316. static int omap_st_start(struct omap_mcbsp *mcbsp)
  317. {
  318. struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
  319. if (st_data && st_data->enabled && !st_data->running) {
  320. omap_st_fir_write(mcbsp, st_data->taps);
  321. omap_st_chgain(mcbsp);
  322. if (!mcbsp->free) {
  323. omap_st_on(mcbsp);
  324. st_data->running = 1;
  325. }
  326. }
  327. return 0;
  328. }
  329. int omap_st_enable(unsigned int id)
  330. {
  331. struct omap_mcbsp *mcbsp;
  332. struct omap_mcbsp_st_data *st_data;
  333. if (!omap_mcbsp_check_valid_id(id)) {
  334. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  335. return -ENODEV;
  336. }
  337. mcbsp = id_to_mcbsp_ptr(id);
  338. st_data = mcbsp->st_data;
  339. if (!st_data)
  340. return -ENODEV;
  341. spin_lock_irq(&mcbsp->lock);
  342. st_data->enabled = 1;
  343. omap_st_start(mcbsp);
  344. spin_unlock_irq(&mcbsp->lock);
  345. return 0;
  346. }
  347. EXPORT_SYMBOL(omap_st_enable);
  348. static int omap_st_stop(struct omap_mcbsp *mcbsp)
  349. {
  350. struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
  351. if (st_data && st_data->running) {
  352. if (!mcbsp->free) {
  353. omap_st_off(mcbsp);
  354. st_data->running = 0;
  355. }
  356. }
  357. return 0;
  358. }
  359. int omap_st_disable(unsigned int id)
  360. {
  361. struct omap_mcbsp *mcbsp;
  362. struct omap_mcbsp_st_data *st_data;
  363. int ret = 0;
  364. if (!omap_mcbsp_check_valid_id(id)) {
  365. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  366. return -ENODEV;
  367. }
  368. mcbsp = id_to_mcbsp_ptr(id);
  369. st_data = mcbsp->st_data;
  370. if (!st_data)
  371. return -ENODEV;
  372. spin_lock_irq(&mcbsp->lock);
  373. omap_st_stop(mcbsp);
  374. st_data->enabled = 0;
  375. spin_unlock_irq(&mcbsp->lock);
  376. return ret;
  377. }
  378. EXPORT_SYMBOL(omap_st_disable);
  379. int omap_st_is_enabled(unsigned int id)
  380. {
  381. struct omap_mcbsp *mcbsp;
  382. struct omap_mcbsp_st_data *st_data;
  383. if (!omap_mcbsp_check_valid_id(id)) {
  384. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  385. return -ENODEV;
  386. }
  387. mcbsp = id_to_mcbsp_ptr(id);
  388. st_data = mcbsp->st_data;
  389. if (!st_data)
  390. return -ENODEV;
  391. return st_data->enabled;
  392. }
  393. EXPORT_SYMBOL(omap_st_is_enabled);
  394. /*
  395. * omap_mcbsp_set_rx_threshold configures the transmit threshold in words.
  396. * The threshold parameter is 1 based, and it is converted (threshold - 1)
  397. * for the THRSH2 register.
  398. */
  399. void omap_mcbsp_set_tx_threshold(unsigned int id, u16 threshold)
  400. {
  401. struct omap_mcbsp *mcbsp;
  402. if (!cpu_is_omap34xx() && !cpu_is_omap44xx())
  403. return;
  404. if (!omap_mcbsp_check_valid_id(id)) {
  405. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  406. return;
  407. }
  408. mcbsp = id_to_mcbsp_ptr(id);
  409. if (threshold && threshold <= mcbsp->max_tx_thres)
  410. MCBSP_WRITE(mcbsp, THRSH2, threshold - 1);
  411. }
  412. EXPORT_SYMBOL(omap_mcbsp_set_tx_threshold);
  413. /*
  414. * omap_mcbsp_set_rx_threshold configures the receive threshold in words.
  415. * The threshold parameter is 1 based, and it is converted (threshold - 1)
  416. * for the THRSH1 register.
  417. */
  418. void omap_mcbsp_set_rx_threshold(unsigned int id, u16 threshold)
  419. {
  420. struct omap_mcbsp *mcbsp;
  421. if (!cpu_is_omap34xx() && !cpu_is_omap44xx())
  422. return;
  423. if (!omap_mcbsp_check_valid_id(id)) {
  424. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  425. return;
  426. }
  427. mcbsp = id_to_mcbsp_ptr(id);
  428. if (threshold && threshold <= mcbsp->max_rx_thres)
  429. MCBSP_WRITE(mcbsp, THRSH1, threshold - 1);
  430. }
  431. EXPORT_SYMBOL(omap_mcbsp_set_rx_threshold);
  432. /*
  433. * omap_mcbsp_get_max_tx_thres just return the current configured
  434. * maximum threshold for transmission
  435. */
  436. u16 omap_mcbsp_get_max_tx_threshold(unsigned int id)
  437. {
  438. struct omap_mcbsp *mcbsp;
  439. if (!omap_mcbsp_check_valid_id(id)) {
  440. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  441. return -ENODEV;
  442. }
  443. mcbsp = id_to_mcbsp_ptr(id);
  444. return mcbsp->max_tx_thres;
  445. }
  446. EXPORT_SYMBOL(omap_mcbsp_get_max_tx_threshold);
  447. /*
  448. * omap_mcbsp_get_max_rx_thres just return the current configured
  449. * maximum threshold for reception
  450. */
  451. u16 omap_mcbsp_get_max_rx_threshold(unsigned int id)
  452. {
  453. struct omap_mcbsp *mcbsp;
  454. if (!omap_mcbsp_check_valid_id(id)) {
  455. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  456. return -ENODEV;
  457. }
  458. mcbsp = id_to_mcbsp_ptr(id);
  459. return mcbsp->max_rx_thres;
  460. }
  461. EXPORT_SYMBOL(omap_mcbsp_get_max_rx_threshold);
  462. u16 omap_mcbsp_get_fifo_size(unsigned int id)
  463. {
  464. struct omap_mcbsp *mcbsp;
  465. if (!omap_mcbsp_check_valid_id(id)) {
  466. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  467. return -ENODEV;
  468. }
  469. mcbsp = id_to_mcbsp_ptr(id);
  470. return mcbsp->pdata->buffer_size;
  471. }
  472. EXPORT_SYMBOL(omap_mcbsp_get_fifo_size);
  473. /*
  474. * omap_mcbsp_get_tx_delay returns the number of used slots in the McBSP FIFO
  475. */
  476. u16 omap_mcbsp_get_tx_delay(unsigned int id)
  477. {
  478. struct omap_mcbsp *mcbsp;
  479. u16 buffstat;
  480. if (!omap_mcbsp_check_valid_id(id)) {
  481. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  482. return -ENODEV;
  483. }
  484. mcbsp = id_to_mcbsp_ptr(id);
  485. /* Returns the number of free locations in the buffer */
  486. buffstat = MCBSP_READ(mcbsp, XBUFFSTAT);
  487. /* Number of slots are different in McBSP ports */
  488. return mcbsp->pdata->buffer_size - buffstat;
  489. }
  490. EXPORT_SYMBOL(omap_mcbsp_get_tx_delay);
  491. /*
  492. * omap_mcbsp_get_rx_delay returns the number of free slots in the McBSP FIFO
  493. * to reach the threshold value (when the DMA will be triggered to read it)
  494. */
  495. u16 omap_mcbsp_get_rx_delay(unsigned int id)
  496. {
  497. struct omap_mcbsp *mcbsp;
  498. u16 buffstat, threshold;
  499. if (!omap_mcbsp_check_valid_id(id)) {
  500. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  501. return -ENODEV;
  502. }
  503. mcbsp = id_to_mcbsp_ptr(id);
  504. /* Returns the number of used locations in the buffer */
  505. buffstat = MCBSP_READ(mcbsp, RBUFFSTAT);
  506. /* RX threshold */
  507. threshold = MCBSP_READ(mcbsp, THRSH1);
  508. /* Return the number of location till we reach the threshold limit */
  509. if (threshold <= buffstat)
  510. return 0;
  511. else
  512. return threshold - buffstat;
  513. }
  514. EXPORT_SYMBOL(omap_mcbsp_get_rx_delay);
  515. /*
  516. * omap_mcbsp_get_dma_op_mode just return the current configured
  517. * operating mode for the mcbsp channel
  518. */
  519. int omap_mcbsp_get_dma_op_mode(unsigned int id)
  520. {
  521. struct omap_mcbsp *mcbsp;
  522. int dma_op_mode;
  523. if (!omap_mcbsp_check_valid_id(id)) {
  524. printk(KERN_ERR "%s: Invalid id (%u)\n", __func__, id + 1);
  525. return -ENODEV;
  526. }
  527. mcbsp = id_to_mcbsp_ptr(id);
  528. dma_op_mode = mcbsp->dma_op_mode;
  529. return dma_op_mode;
  530. }
  531. EXPORT_SYMBOL(omap_mcbsp_get_dma_op_mode);
  532. static inline void omap34xx_mcbsp_request(struct omap_mcbsp *mcbsp)
  533. {
  534. struct omap_device *od;
  535. od = find_omap_device_by_dev(mcbsp->dev);
  536. /*
  537. * Enable wakup behavior, smart idle and all wakeups
  538. * REVISIT: some wakeups may be unnecessary
  539. */
  540. if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
  541. MCBSP_WRITE(mcbsp, WAKEUPEN, XRDYEN | RRDYEN);
  542. }
  543. }
  544. static inline void omap34xx_mcbsp_free(struct omap_mcbsp *mcbsp)
  545. {
  546. struct omap_device *od;
  547. od = find_omap_device_by_dev(mcbsp->dev);
  548. /*
  549. * Disable wakup behavior, smart idle and all wakeups
  550. */
  551. if (cpu_is_omap34xx() || cpu_is_omap44xx()) {
  552. /*
  553. * HW bug workaround - If no_idle mode is taken, we need to
  554. * go to smart_idle before going to always_idle, or the
  555. * device will not hit retention anymore.
  556. */
  557. MCBSP_WRITE(mcbsp, WAKEUPEN, 0);
  558. }
  559. }
  560. #else
  561. static inline void omap34xx_mcbsp_request(struct omap_mcbsp *mcbsp) {}
  562. static inline void omap34xx_mcbsp_free(struct omap_mcbsp *mcbsp) {}
  563. static inline void omap_st_start(struct omap_mcbsp *mcbsp) {}
  564. static inline void omap_st_stop(struct omap_mcbsp *mcbsp) {}
  565. #endif
  566. /*
  567. * We can choose between IRQ based or polled IO.
  568. * This needs to be called before omap_mcbsp_request().
  569. */
  570. int omap_mcbsp_set_io_type(unsigned int id, omap_mcbsp_io_type_t io_type)
  571. {
  572. struct omap_mcbsp *mcbsp;
  573. if (!omap_mcbsp_check_valid_id(id)) {
  574. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  575. return -ENODEV;
  576. }
  577. mcbsp = id_to_mcbsp_ptr(id);
  578. spin_lock(&mcbsp->lock);
  579. if (!mcbsp->free) {
  580. dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
  581. mcbsp->id);
  582. spin_unlock(&mcbsp->lock);
  583. return -EINVAL;
  584. }
  585. mcbsp->io_type = io_type;
  586. spin_unlock(&mcbsp->lock);
  587. return 0;
  588. }
  589. EXPORT_SYMBOL(omap_mcbsp_set_io_type);
  590. int omap_mcbsp_request(unsigned int id)
  591. {
  592. struct omap_mcbsp *mcbsp;
  593. void *reg_cache;
  594. int err;
  595. if (!omap_mcbsp_check_valid_id(id)) {
  596. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  597. return -ENODEV;
  598. }
  599. mcbsp = id_to_mcbsp_ptr(id);
  600. reg_cache = kzalloc(omap_mcbsp_cache_size, GFP_KERNEL);
  601. if (!reg_cache) {
  602. return -ENOMEM;
  603. }
  604. spin_lock(&mcbsp->lock);
  605. if (!mcbsp->free) {
  606. dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
  607. mcbsp->id);
  608. err = -EBUSY;
  609. goto err_kfree;
  610. }
  611. mcbsp->free = false;
  612. mcbsp->reg_cache = reg_cache;
  613. spin_unlock(&mcbsp->lock);
  614. if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->request)
  615. mcbsp->pdata->ops->request(id);
  616. clk_enable(mcbsp->iclk);
  617. clk_enable(mcbsp->fclk);
  618. /* Do procedure specific to omap34xx arch, if applicable */
  619. omap34xx_mcbsp_request(mcbsp);
  620. /*
  621. * Make sure that transmitter, receiver and sample-rate generator are
  622. * not running before activating IRQs.
  623. */
  624. MCBSP_WRITE(mcbsp, SPCR1, 0);
  625. MCBSP_WRITE(mcbsp, SPCR2, 0);
  626. if (mcbsp->io_type == OMAP_MCBSP_IRQ_IO) {
  627. /* We need to get IRQs here */
  628. init_completion(&mcbsp->tx_irq_completion);
  629. err = request_irq(mcbsp->tx_irq, omap_mcbsp_tx_irq_handler,
  630. 0, "McBSP", (void *)mcbsp);
  631. if (err != 0) {
  632. dev_err(mcbsp->dev, "Unable to request TX IRQ %d "
  633. "for McBSP%d\n", mcbsp->tx_irq,
  634. mcbsp->id);
  635. goto err_clk_disable;
  636. }
  637. if (mcbsp->rx_irq) {
  638. init_completion(&mcbsp->rx_irq_completion);
  639. err = request_irq(mcbsp->rx_irq,
  640. omap_mcbsp_rx_irq_handler,
  641. 0, "McBSP", (void *)mcbsp);
  642. if (err != 0) {
  643. dev_err(mcbsp->dev, "Unable to request RX IRQ %d "
  644. "for McBSP%d\n", mcbsp->rx_irq,
  645. mcbsp->id);
  646. goto err_free_irq;
  647. }
  648. }
  649. }
  650. return 0;
  651. err_free_irq:
  652. free_irq(mcbsp->tx_irq, (void *)mcbsp);
  653. err_clk_disable:
  654. if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
  655. mcbsp->pdata->ops->free(id);
  656. /* Do procedure specific to omap34xx arch, if applicable */
  657. omap34xx_mcbsp_free(mcbsp);
  658. clk_disable(mcbsp->fclk);
  659. clk_disable(mcbsp->iclk);
  660. spin_lock(&mcbsp->lock);
  661. mcbsp->free = true;
  662. mcbsp->reg_cache = NULL;
  663. err_kfree:
  664. spin_unlock(&mcbsp->lock);
  665. kfree(reg_cache);
  666. return err;
  667. }
  668. EXPORT_SYMBOL(omap_mcbsp_request);
  669. void omap_mcbsp_free(unsigned int id)
  670. {
  671. struct omap_mcbsp *mcbsp;
  672. void *reg_cache;
  673. if (!omap_mcbsp_check_valid_id(id)) {
  674. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  675. return;
  676. }
  677. mcbsp = id_to_mcbsp_ptr(id);
  678. if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
  679. mcbsp->pdata->ops->free(id);
  680. /* Do procedure specific to omap34xx arch, if applicable */
  681. omap34xx_mcbsp_free(mcbsp);
  682. clk_disable(mcbsp->fclk);
  683. clk_disable(mcbsp->iclk);
  684. if (mcbsp->io_type == OMAP_MCBSP_IRQ_IO) {
  685. /* Free IRQs */
  686. if (mcbsp->rx_irq)
  687. free_irq(mcbsp->rx_irq, (void *)mcbsp);
  688. free_irq(mcbsp->tx_irq, (void *)mcbsp);
  689. }
  690. reg_cache = mcbsp->reg_cache;
  691. spin_lock(&mcbsp->lock);
  692. if (mcbsp->free)
  693. dev_err(mcbsp->dev, "McBSP%d was not reserved\n", mcbsp->id);
  694. else
  695. mcbsp->free = true;
  696. mcbsp->reg_cache = NULL;
  697. spin_unlock(&mcbsp->lock);
  698. if (reg_cache)
  699. kfree(reg_cache);
  700. }
  701. EXPORT_SYMBOL(omap_mcbsp_free);
  702. /*
  703. * Here we start the McBSP, by enabling transmitter, receiver or both.
  704. * If no transmitter or receiver is active prior calling, then sample-rate
  705. * generator and frame sync are started.
  706. */
  707. void omap_mcbsp_start(unsigned int id, int tx, int rx)
  708. {
  709. struct omap_mcbsp *mcbsp;
  710. int enable_srg = 0;
  711. u16 w;
  712. if (!omap_mcbsp_check_valid_id(id)) {
  713. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  714. return;
  715. }
  716. mcbsp = id_to_mcbsp_ptr(id);
  717. if (cpu_is_omap34xx())
  718. omap_st_start(mcbsp);
  719. mcbsp->rx_word_length = (MCBSP_READ_CACHE(mcbsp, RCR1) >> 5) & 0x7;
  720. mcbsp->tx_word_length = (MCBSP_READ_CACHE(mcbsp, XCR1) >> 5) & 0x7;
  721. /* Only enable SRG, if McBSP is master */
  722. w = MCBSP_READ_CACHE(mcbsp, PCR0);
  723. if (w & (FSXM | FSRM | CLKXM | CLKRM))
  724. enable_srg = !((MCBSP_READ_CACHE(mcbsp, SPCR2) |
  725. MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1);
  726. if (enable_srg) {
  727. /* Start the sample generator */
  728. w = MCBSP_READ_CACHE(mcbsp, SPCR2);
  729. MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 6));
  730. }
  731. /* Enable transmitter and receiver */
  732. tx &= 1;
  733. w = MCBSP_READ_CACHE(mcbsp, SPCR2);
  734. MCBSP_WRITE(mcbsp, SPCR2, w | tx);
  735. rx &= 1;
  736. w = MCBSP_READ_CACHE(mcbsp, SPCR1);
  737. MCBSP_WRITE(mcbsp, SPCR1, w | rx);
  738. /*
  739. * Worst case: CLKSRG*2 = 8000khz: (1/8000) * 2 * 2 usec
  740. * REVISIT: 100us may give enough time for two CLKSRG, however
  741. * due to some unknown PM related, clock gating etc. reason it
  742. * is now at 500us.
  743. */
  744. udelay(500);
  745. if (enable_srg) {
  746. /* Start frame sync */
  747. w = MCBSP_READ_CACHE(mcbsp, SPCR2);
  748. MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 7));
  749. }
  750. if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
  751. /* Release the transmitter and receiver */
  752. w = MCBSP_READ_CACHE(mcbsp, XCCR);
  753. w &= ~(tx ? XDISABLE : 0);
  754. MCBSP_WRITE(mcbsp, XCCR, w);
  755. w = MCBSP_READ_CACHE(mcbsp, RCCR);
  756. w &= ~(rx ? RDISABLE : 0);
  757. MCBSP_WRITE(mcbsp, RCCR, w);
  758. }
  759. /* Dump McBSP Regs */
  760. omap_mcbsp_dump_reg(id);
  761. }
  762. EXPORT_SYMBOL(omap_mcbsp_start);
  763. void omap_mcbsp_stop(unsigned int id, int tx, int rx)
  764. {
  765. struct omap_mcbsp *mcbsp;
  766. int idle;
  767. u16 w;
  768. if (!omap_mcbsp_check_valid_id(id)) {
  769. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  770. return;
  771. }
  772. mcbsp = id_to_mcbsp_ptr(id);
  773. /* Reset transmitter */
  774. tx &= 1;
  775. if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
  776. w = MCBSP_READ_CACHE(mcbsp, XCCR);
  777. w |= (tx ? XDISABLE : 0);
  778. MCBSP_WRITE(mcbsp, XCCR, w);
  779. }
  780. w = MCBSP_READ_CACHE(mcbsp, SPCR2);
  781. MCBSP_WRITE(mcbsp, SPCR2, w & ~tx);
  782. /* Reset receiver */
  783. rx &= 1;
  784. if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
  785. w = MCBSP_READ_CACHE(mcbsp, RCCR);
  786. w |= (rx ? RDISABLE : 0);
  787. MCBSP_WRITE(mcbsp, RCCR, w);
  788. }
  789. w = MCBSP_READ_CACHE(mcbsp, SPCR1);
  790. MCBSP_WRITE(mcbsp, SPCR1, w & ~rx);
  791. idle = !((MCBSP_READ_CACHE(mcbsp, SPCR2) |
  792. MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1);
  793. if (idle) {
  794. /* Reset the sample rate generator */
  795. w = MCBSP_READ_CACHE(mcbsp, SPCR2);
  796. MCBSP_WRITE(mcbsp, SPCR2, w & ~(1 << 6));
  797. }
  798. if (cpu_is_omap34xx())
  799. omap_st_stop(mcbsp);
  800. }
  801. EXPORT_SYMBOL(omap_mcbsp_stop);
  802. /* polled mcbsp i/o operations */
  803. int omap_mcbsp_pollwrite(unsigned int id, u16 buf)
  804. {
  805. struct omap_mcbsp *mcbsp;
  806. if (!omap_mcbsp_check_valid_id(id)) {
  807. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  808. return -ENODEV;
  809. }
  810. mcbsp = id_to_mcbsp_ptr(id);
  811. MCBSP_WRITE(mcbsp, DXR1, buf);
  812. /* if frame sync error - clear the error */
  813. if (MCBSP_READ(mcbsp, SPCR2) & XSYNC_ERR) {
  814. /* clear error */
  815. MCBSP_WRITE(mcbsp, SPCR2, MCBSP_READ_CACHE(mcbsp, SPCR2));
  816. /* resend */
  817. return -1;
  818. } else {
  819. /* wait for transmit confirmation */
  820. int attemps = 0;
  821. while (!(MCBSP_READ(mcbsp, SPCR2) & XRDY)) {
  822. if (attemps++ > 1000) {
  823. MCBSP_WRITE(mcbsp, SPCR2,
  824. MCBSP_READ_CACHE(mcbsp, SPCR2) &
  825. (~XRST));
  826. udelay(10);
  827. MCBSP_WRITE(mcbsp, SPCR2,
  828. MCBSP_READ_CACHE(mcbsp, SPCR2) |
  829. (XRST));
  830. udelay(10);
  831. dev_err(mcbsp->dev, "Could not write to"
  832. " McBSP%d Register\n", mcbsp->id);
  833. return -2;
  834. }
  835. }
  836. }
  837. return 0;
  838. }
  839. EXPORT_SYMBOL(omap_mcbsp_pollwrite);
  840. int omap_mcbsp_pollread(unsigned int id, u16 *buf)
  841. {
  842. struct omap_mcbsp *mcbsp;
  843. if (!omap_mcbsp_check_valid_id(id)) {
  844. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  845. return -ENODEV;
  846. }
  847. mcbsp = id_to_mcbsp_ptr(id);
  848. /* if frame sync error - clear the error */
  849. if (MCBSP_READ(mcbsp, SPCR1) & RSYNC_ERR) {
  850. /* clear error */
  851. MCBSP_WRITE(mcbsp, SPCR1, MCBSP_READ_CACHE(mcbsp, SPCR1));
  852. /* resend */
  853. return -1;
  854. } else {
  855. /* wait for recieve confirmation */
  856. int attemps = 0;
  857. while (!(MCBSP_READ(mcbsp, SPCR1) & RRDY)) {
  858. if (attemps++ > 1000) {
  859. MCBSP_WRITE(mcbsp, SPCR1,
  860. MCBSP_READ_CACHE(mcbsp, SPCR1) &
  861. (~RRST));
  862. udelay(10);
  863. MCBSP_WRITE(mcbsp, SPCR1,
  864. MCBSP_READ_CACHE(mcbsp, SPCR1) |
  865. (RRST));
  866. udelay(10);
  867. dev_err(mcbsp->dev, "Could not read from"
  868. " McBSP%d Register\n", mcbsp->id);
  869. return -2;
  870. }
  871. }
  872. }
  873. *buf = MCBSP_READ(mcbsp, DRR1);
  874. return 0;
  875. }
  876. EXPORT_SYMBOL(omap_mcbsp_pollread);
  877. /*
  878. * IRQ based word transmission.
  879. */
  880. void omap_mcbsp_xmit_word(unsigned int id, u32 word)
  881. {
  882. struct omap_mcbsp *mcbsp;
  883. omap_mcbsp_word_length word_length;
  884. if (!omap_mcbsp_check_valid_id(id)) {
  885. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  886. return;
  887. }
  888. mcbsp = id_to_mcbsp_ptr(id);
  889. word_length = mcbsp->tx_word_length;
  890. wait_for_completion(&mcbsp->tx_irq_completion);
  891. if (word_length > OMAP_MCBSP_WORD_16)
  892. MCBSP_WRITE(mcbsp, DXR2, word >> 16);
  893. MCBSP_WRITE(mcbsp, DXR1, word & 0xffff);
  894. }
  895. EXPORT_SYMBOL(omap_mcbsp_xmit_word);
  896. u32 omap_mcbsp_recv_word(unsigned int id)
  897. {
  898. struct omap_mcbsp *mcbsp;
  899. u16 word_lsb, word_msb = 0;
  900. omap_mcbsp_word_length word_length;
  901. if (!omap_mcbsp_check_valid_id(id)) {
  902. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  903. return -ENODEV;
  904. }
  905. mcbsp = id_to_mcbsp_ptr(id);
  906. word_length = mcbsp->rx_word_length;
  907. wait_for_completion(&mcbsp->rx_irq_completion);
  908. if (word_length > OMAP_MCBSP_WORD_16)
  909. word_msb = MCBSP_READ(mcbsp, DRR2);
  910. word_lsb = MCBSP_READ(mcbsp, DRR1);
  911. return (word_lsb | (word_msb << 16));
  912. }
  913. EXPORT_SYMBOL(omap_mcbsp_recv_word);
  914. int omap_mcbsp_spi_master_xmit_word_poll(unsigned int id, u32 word)
  915. {
  916. struct omap_mcbsp *mcbsp;
  917. omap_mcbsp_word_length tx_word_length;
  918. omap_mcbsp_word_length rx_word_length;
  919. u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
  920. if (!omap_mcbsp_check_valid_id(id)) {
  921. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  922. return -ENODEV;
  923. }
  924. mcbsp = id_to_mcbsp_ptr(id);
  925. tx_word_length = mcbsp->tx_word_length;
  926. rx_word_length = mcbsp->rx_word_length;
  927. if (tx_word_length != rx_word_length)
  928. return -EINVAL;
  929. /* First we wait for the transmitter to be ready */
  930. spcr2 = MCBSP_READ(mcbsp, SPCR2);
  931. while (!(spcr2 & XRDY)) {
  932. spcr2 = MCBSP_READ(mcbsp, SPCR2);
  933. if (attempts++ > 1000) {
  934. /* We must reset the transmitter */
  935. MCBSP_WRITE(mcbsp, SPCR2,
  936. MCBSP_READ_CACHE(mcbsp, SPCR2) & (~XRST));
  937. udelay(10);
  938. MCBSP_WRITE(mcbsp, SPCR2,
  939. MCBSP_READ_CACHE(mcbsp, SPCR2) | XRST);
  940. udelay(10);
  941. dev_err(mcbsp->dev, "McBSP%d transmitter not "
  942. "ready\n", mcbsp->id);
  943. return -EAGAIN;
  944. }
  945. }
  946. /* Now we can push the data */
  947. if (tx_word_length > OMAP_MCBSP_WORD_16)
  948. MCBSP_WRITE(mcbsp, DXR2, word >> 16);
  949. MCBSP_WRITE(mcbsp, DXR1, word & 0xffff);
  950. /* We wait for the receiver to be ready */
  951. spcr1 = MCBSP_READ(mcbsp, SPCR1);
  952. while (!(spcr1 & RRDY)) {
  953. spcr1 = MCBSP_READ(mcbsp, SPCR1);
  954. if (attempts++ > 1000) {
  955. /* We must reset the receiver */
  956. MCBSP_WRITE(mcbsp, SPCR1,
  957. MCBSP_READ_CACHE(mcbsp, SPCR1) & (~RRST));
  958. udelay(10);
  959. MCBSP_WRITE(mcbsp, SPCR1,
  960. MCBSP_READ_CACHE(mcbsp, SPCR1) | RRST);
  961. udelay(10);
  962. dev_err(mcbsp->dev, "McBSP%d receiver not "
  963. "ready\n", mcbsp->id);
  964. return -EAGAIN;
  965. }
  966. }
  967. /* Receiver is ready, let's read the dummy data */
  968. if (rx_word_length > OMAP_MCBSP_WORD_16)
  969. word_msb = MCBSP_READ(mcbsp, DRR2);
  970. word_lsb = MCBSP_READ(mcbsp, DRR1);
  971. return 0;
  972. }
  973. EXPORT_SYMBOL(omap_mcbsp_spi_master_xmit_word_poll);
  974. int omap_mcbsp_spi_master_recv_word_poll(unsigned int id, u32 *word)
  975. {
  976. struct omap_mcbsp *mcbsp;
  977. u32 clock_word = 0;
  978. omap_mcbsp_word_length tx_word_length;
  979. omap_mcbsp_word_length rx_word_length;
  980. u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
  981. if (!omap_mcbsp_check_valid_id(id)) {
  982. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  983. return -ENODEV;
  984. }
  985. mcbsp = id_to_mcbsp_ptr(id);
  986. tx_word_length = mcbsp->tx_word_length;
  987. rx_word_length = mcbsp->rx_word_length;
  988. if (tx_word_length != rx_word_length)
  989. return -EINVAL;
  990. /* First we wait for the transmitter to be ready */
  991. spcr2 = MCBSP_READ(mcbsp, SPCR2);
  992. while (!(spcr2 & XRDY)) {
  993. spcr2 = MCBSP_READ(mcbsp, SPCR2);
  994. if (attempts++ > 1000) {
  995. /* We must reset the transmitter */
  996. MCBSP_WRITE(mcbsp, SPCR2,
  997. MCBSP_READ_CACHE(mcbsp, SPCR2) & (~XRST));
  998. udelay(10);
  999. MCBSP_WRITE(mcbsp, SPCR2,
  1000. MCBSP_READ_CACHE(mcbsp, SPCR2) | XRST);
  1001. udelay(10);
  1002. dev_err(mcbsp->dev, "McBSP%d transmitter not "
  1003. "ready\n", mcbsp->id);
  1004. return -EAGAIN;
  1005. }
  1006. }
  1007. /* We first need to enable the bus clock */
  1008. if (tx_word_length > OMAP_MCBSP_WORD_16)
  1009. MCBSP_WRITE(mcbsp, DXR2, clock_word >> 16);
  1010. MCBSP_WRITE(mcbsp, DXR1, clock_word & 0xffff);
  1011. /* We wait for the receiver to be ready */
  1012. spcr1 = MCBSP_READ(mcbsp, SPCR1);
  1013. while (!(spcr1 & RRDY)) {
  1014. spcr1 = MCBSP_READ(mcbsp, SPCR1);
  1015. if (attempts++ > 1000) {
  1016. /* We must reset the receiver */
  1017. MCBSP_WRITE(mcbsp, SPCR1,
  1018. MCBSP_READ_CACHE(mcbsp, SPCR1) & (~RRST));
  1019. udelay(10);
  1020. MCBSP_WRITE(mcbsp, SPCR1,
  1021. MCBSP_READ_CACHE(mcbsp, SPCR1) | RRST);
  1022. udelay(10);
  1023. dev_err(mcbsp->dev, "McBSP%d receiver not "
  1024. "ready\n", mcbsp->id);
  1025. return -EAGAIN;
  1026. }
  1027. }
  1028. /* Receiver is ready, there is something for us */
  1029. if (rx_word_length > OMAP_MCBSP_WORD_16)
  1030. word_msb = MCBSP_READ(mcbsp, DRR2);
  1031. word_lsb = MCBSP_READ(mcbsp, DRR1);
  1032. word[0] = (word_lsb | (word_msb << 16));
  1033. return 0;
  1034. }
  1035. EXPORT_SYMBOL(omap_mcbsp_spi_master_recv_word_poll);
  1036. /*
  1037. * Simple DMA based buffer rx/tx routines.
  1038. * Nothing fancy, just a single buffer tx/rx through DMA.
  1039. * The DMA resources are released once the transfer is done.
  1040. * For anything fancier, you should use your own customized DMA
  1041. * routines and callbacks.
  1042. */
  1043. int omap_mcbsp_xmit_buffer(unsigned int id, dma_addr_t buffer,
  1044. unsigned int length)
  1045. {
  1046. struct omap_mcbsp *mcbsp;
  1047. int dma_tx_ch;
  1048. int src_port = 0;
  1049. int dest_port = 0;
  1050. int sync_dev = 0;
  1051. if (!omap_mcbsp_check_valid_id(id)) {
  1052. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  1053. return -ENODEV;
  1054. }
  1055. mcbsp = id_to_mcbsp_ptr(id);
  1056. if (omap_request_dma(mcbsp->dma_tx_sync, "McBSP TX",
  1057. omap_mcbsp_tx_dma_callback,
  1058. mcbsp,
  1059. &dma_tx_ch)) {
  1060. dev_err(mcbsp->dev, " Unable to request DMA channel for "
  1061. "McBSP%d TX. Trying IRQ based TX\n",
  1062. mcbsp->id);
  1063. return -EAGAIN;
  1064. }
  1065. mcbsp->dma_tx_lch = dma_tx_ch;
  1066. dev_err(mcbsp->dev, "McBSP%d TX DMA on channel %d\n", mcbsp->id,
  1067. dma_tx_ch);
  1068. init_completion(&mcbsp->tx_dma_completion);
  1069. if (cpu_class_is_omap1()) {
  1070. src_port = OMAP_DMA_PORT_TIPB;
  1071. dest_port = OMAP_DMA_PORT_EMIFF;
  1072. }
  1073. if (cpu_class_is_omap2())
  1074. sync_dev = mcbsp->dma_tx_sync;
  1075. omap_set_dma_transfer_params(mcbsp->dma_tx_lch,
  1076. OMAP_DMA_DATA_TYPE_S16,
  1077. length >> 1, 1,
  1078. OMAP_DMA_SYNC_ELEMENT,
  1079. sync_dev, 0);
  1080. omap_set_dma_dest_params(mcbsp->dma_tx_lch,
  1081. src_port,
  1082. OMAP_DMA_AMODE_CONSTANT,
  1083. mcbsp->phys_base + OMAP_MCBSP_REG_DXR1,
  1084. 0, 0);
  1085. omap_set_dma_src_params(mcbsp->dma_tx_lch,
  1086. dest_port,
  1087. OMAP_DMA_AMODE_POST_INC,
  1088. buffer,
  1089. 0, 0);
  1090. omap_start_dma(mcbsp->dma_tx_lch);
  1091. wait_for_completion(&mcbsp->tx_dma_completion);
  1092. return 0;
  1093. }
  1094. EXPORT_SYMBOL(omap_mcbsp_xmit_buffer);
  1095. int omap_mcbsp_recv_buffer(unsigned int id, dma_addr_t buffer,
  1096. unsigned int length)
  1097. {
  1098. struct omap_mcbsp *mcbsp;
  1099. int dma_rx_ch;
  1100. int src_port = 0;
  1101. int dest_port = 0;
  1102. int sync_dev = 0;
  1103. if (!omap_mcbsp_check_valid_id(id)) {
  1104. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  1105. return -ENODEV;
  1106. }
  1107. mcbsp = id_to_mcbsp_ptr(id);
  1108. if (omap_request_dma(mcbsp->dma_rx_sync, "McBSP RX",
  1109. omap_mcbsp_rx_dma_callback,
  1110. mcbsp,
  1111. &dma_rx_ch)) {
  1112. dev_err(mcbsp->dev, "Unable to request DMA channel for "
  1113. "McBSP%d RX. Trying IRQ based RX\n",
  1114. mcbsp->id);
  1115. return -EAGAIN;
  1116. }
  1117. mcbsp->dma_rx_lch = dma_rx_ch;
  1118. dev_err(mcbsp->dev, "McBSP%d RX DMA on channel %d\n", mcbsp->id,
  1119. dma_rx_ch);
  1120. init_completion(&mcbsp->rx_dma_completion);
  1121. if (cpu_class_is_omap1()) {
  1122. src_port = OMAP_DMA_PORT_TIPB;
  1123. dest_port = OMAP_DMA_PORT_EMIFF;
  1124. }
  1125. if (cpu_class_is_omap2())
  1126. sync_dev = mcbsp->dma_rx_sync;
  1127. omap_set_dma_transfer_params(mcbsp->dma_rx_lch,
  1128. OMAP_DMA_DATA_TYPE_S16,
  1129. length >> 1, 1,
  1130. OMAP_DMA_SYNC_ELEMENT,
  1131. sync_dev, 0);
  1132. omap_set_dma_src_params(mcbsp->dma_rx_lch,
  1133. src_port,
  1134. OMAP_DMA_AMODE_CONSTANT,
  1135. mcbsp->phys_base + OMAP_MCBSP_REG_DRR1,
  1136. 0, 0);
  1137. omap_set_dma_dest_params(mcbsp->dma_rx_lch,
  1138. dest_port,
  1139. OMAP_DMA_AMODE_POST_INC,
  1140. buffer,
  1141. 0, 0);
  1142. omap_start_dma(mcbsp->dma_rx_lch);
  1143. wait_for_completion(&mcbsp->rx_dma_completion);
  1144. return 0;
  1145. }
  1146. EXPORT_SYMBOL(omap_mcbsp_recv_buffer);
  1147. /*
  1148. * SPI wrapper.
  1149. * Since SPI setup is much simpler than the generic McBSP one,
  1150. * this wrapper just need an omap_mcbsp_spi_cfg structure as an input.
  1151. * Once this is done, you can call omap_mcbsp_start().
  1152. */
  1153. void omap_mcbsp_set_spi_mode(unsigned int id,
  1154. const struct omap_mcbsp_spi_cfg *spi_cfg)
  1155. {
  1156. struct omap_mcbsp *mcbsp;
  1157. struct omap_mcbsp_reg_cfg mcbsp_cfg;
  1158. if (!omap_mcbsp_check_valid_id(id)) {
  1159. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  1160. return;
  1161. }
  1162. mcbsp = id_to_mcbsp_ptr(id);
  1163. memset(&mcbsp_cfg, 0, sizeof(struct omap_mcbsp_reg_cfg));
  1164. /* SPI has only one frame */
  1165. mcbsp_cfg.rcr1 |= (RWDLEN1(spi_cfg->word_length) | RFRLEN1(0));
  1166. mcbsp_cfg.xcr1 |= (XWDLEN1(spi_cfg->word_length) | XFRLEN1(0));
  1167. /* Clock stop mode */
  1168. if (spi_cfg->clk_stp_mode == OMAP_MCBSP_CLK_STP_MODE_NO_DELAY)
  1169. mcbsp_cfg.spcr1 |= (1 << 12);
  1170. else
  1171. mcbsp_cfg.spcr1 |= (3 << 11);
  1172. /* Set clock parities */
  1173. if (spi_cfg->rx_clock_polarity == OMAP_MCBSP_CLK_RISING)
  1174. mcbsp_cfg.pcr0 |= CLKRP;
  1175. else
  1176. mcbsp_cfg.pcr0 &= ~CLKRP;
  1177. if (spi_cfg->tx_clock_polarity == OMAP_MCBSP_CLK_RISING)
  1178. mcbsp_cfg.pcr0 &= ~CLKXP;
  1179. else
  1180. mcbsp_cfg.pcr0 |= CLKXP;
  1181. /* Set SCLKME to 0 and CLKSM to 1 */
  1182. mcbsp_cfg.pcr0 &= ~SCLKME;
  1183. mcbsp_cfg.srgr2 |= CLKSM;
  1184. /* Set FSXP */
  1185. if (spi_cfg->fsx_polarity == OMAP_MCBSP_FS_ACTIVE_HIGH)
  1186. mcbsp_cfg.pcr0 &= ~FSXP;
  1187. else
  1188. mcbsp_cfg.pcr0 |= FSXP;
  1189. if (spi_cfg->spi_mode == OMAP_MCBSP_SPI_MASTER) {
  1190. mcbsp_cfg.pcr0 |= CLKXM;
  1191. mcbsp_cfg.srgr1 |= CLKGDV(spi_cfg->clk_div - 1);
  1192. mcbsp_cfg.pcr0 |= FSXM;
  1193. mcbsp_cfg.srgr2 &= ~FSGM;
  1194. mcbsp_cfg.xcr2 |= XDATDLY(1);
  1195. mcbsp_cfg.rcr2 |= RDATDLY(1);
  1196. } else {
  1197. mcbsp_cfg.pcr0 &= ~CLKXM;
  1198. mcbsp_cfg.srgr1 |= CLKGDV(1);
  1199. mcbsp_cfg.pcr0 &= ~FSXM;
  1200. mcbsp_cfg.xcr2 &= ~XDATDLY(3);
  1201. mcbsp_cfg.rcr2 &= ~RDATDLY(3);
  1202. }
  1203. mcbsp_cfg.xcr2 &= ~XPHASE;
  1204. mcbsp_cfg.rcr2 &= ~RPHASE;
  1205. omap_mcbsp_config(id, &mcbsp_cfg);
  1206. }
  1207. EXPORT_SYMBOL(omap_mcbsp_set_spi_mode);
  1208. #ifdef CONFIG_ARCH_OMAP3
  1209. #define max_thres(m) (mcbsp->pdata->buffer_size)
  1210. #define valid_threshold(m, val) ((val) <= max_thres(m))
  1211. #define THRESHOLD_PROP_BUILDER(prop) \
  1212. static ssize_t prop##_show(struct device *dev, \
  1213. struct device_attribute *attr, char *buf) \
  1214. { \
  1215. struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
  1216. \
  1217. return sprintf(buf, "%u\n", mcbsp->prop); \
  1218. } \
  1219. \
  1220. static ssize_t prop##_store(struct device *dev, \
  1221. struct device_attribute *attr, \
  1222. const char *buf, size_t size) \
  1223. { \
  1224. struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
  1225. unsigned long val; \
  1226. int status; \
  1227. \
  1228. status = strict_strtoul(buf, 0, &val); \
  1229. if (status) \
  1230. return status; \
  1231. \
  1232. if (!valid_threshold(mcbsp, val)) \
  1233. return -EDOM; \
  1234. \
  1235. mcbsp->prop = val; \
  1236. return size; \
  1237. } \
  1238. \
  1239. static DEVICE_ATTR(prop, 0644, prop##_show, prop##_store);
  1240. THRESHOLD_PROP_BUILDER(max_tx_thres);
  1241. THRESHOLD_PROP_BUILDER(max_rx_thres);
  1242. static const char *dma_op_modes[] = {
  1243. "element", "threshold", "frame",
  1244. };
  1245. static ssize_t dma_op_mode_show(struct device *dev,
  1246. struct device_attribute *attr, char *buf)
  1247. {
  1248. struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
  1249. int dma_op_mode, i = 0;
  1250. ssize_t len = 0;
  1251. const char * const *s;
  1252. dma_op_mode = mcbsp->dma_op_mode;
  1253. for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++) {
  1254. if (dma_op_mode == i)
  1255. len += sprintf(buf + len, "[%s] ", *s);
  1256. else
  1257. len += sprintf(buf + len, "%s ", *s);
  1258. }
  1259. len += sprintf(buf + len, "\n");
  1260. return len;
  1261. }
  1262. static ssize_t dma_op_mode_store(struct device *dev,
  1263. struct device_attribute *attr,
  1264. const char *buf, size_t size)
  1265. {
  1266. struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
  1267. const char * const *s;
  1268. int i = 0;
  1269. for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++)
  1270. if (sysfs_streq(buf, *s))
  1271. break;
  1272. if (i == ARRAY_SIZE(dma_op_modes))
  1273. return -EINVAL;
  1274. spin_lock_irq(&mcbsp->lock);
  1275. if (!mcbsp->free) {
  1276. size = -EBUSY;
  1277. goto unlock;
  1278. }
  1279. mcbsp->dma_op_mode = i;
  1280. unlock:
  1281. spin_unlock_irq(&mcbsp->lock);
  1282. return size;
  1283. }
  1284. static DEVICE_ATTR(dma_op_mode, 0644, dma_op_mode_show, dma_op_mode_store);
  1285. static ssize_t st_taps_show(struct device *dev,
  1286. struct device_attribute *attr, char *buf)
  1287. {
  1288. struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
  1289. struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
  1290. ssize_t status = 0;
  1291. int i;
  1292. spin_lock_irq(&mcbsp->lock);
  1293. for (i = 0; i < st_data->nr_taps; i++)
  1294. status += sprintf(&buf[status], (i ? ", %d" : "%d"),
  1295. st_data->taps[i]);
  1296. if (i)
  1297. status += sprintf(&buf[status], "\n");
  1298. spin_unlock_irq(&mcbsp->lock);
  1299. return status;
  1300. }
  1301. static ssize_t st_taps_store(struct device *dev,
  1302. struct device_attribute *attr,
  1303. const char *buf, size_t size)
  1304. {
  1305. struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
  1306. struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
  1307. int val, tmp, status, i = 0;
  1308. spin_lock_irq(&mcbsp->lock);
  1309. memset(st_data->taps, 0, sizeof(st_data->taps));
  1310. st_data->nr_taps = 0;
  1311. do {
  1312. status = sscanf(buf, "%d%n", &val, &tmp);
  1313. if (status < 0 || status == 0) {
  1314. size = -EINVAL;
  1315. goto out;
  1316. }
  1317. if (val < -32768 || val > 32767) {
  1318. size = -EINVAL;
  1319. goto out;
  1320. }
  1321. st_data->taps[i++] = val;
  1322. buf += tmp;
  1323. if (*buf != ',')
  1324. break;
  1325. buf++;
  1326. } while (1);
  1327. st_data->nr_taps = i;
  1328. out:
  1329. spin_unlock_irq(&mcbsp->lock);
  1330. return size;
  1331. }
  1332. static DEVICE_ATTR(st_taps, 0644, st_taps_show, st_taps_store);
  1333. static const struct attribute *additional_attrs[] = {
  1334. &dev_attr_max_tx_thres.attr,
  1335. &dev_attr_max_rx_thres.attr,
  1336. &dev_attr_dma_op_mode.attr,
  1337. NULL,
  1338. };
  1339. static const struct attribute_group additional_attr_group = {
  1340. .attrs = (struct attribute **)additional_attrs,
  1341. };
  1342. static inline int __devinit omap_additional_add(struct device *dev)
  1343. {
  1344. return sysfs_create_group(&dev->kobj, &additional_attr_group);
  1345. }
  1346. static inline void __devexit omap_additional_remove(struct device *dev)
  1347. {
  1348. sysfs_remove_group(&dev->kobj, &additional_attr_group);
  1349. }
  1350. static const struct attribute *sidetone_attrs[] = {
  1351. &dev_attr_st_taps.attr,
  1352. NULL,
  1353. };
  1354. static const struct attribute_group sidetone_attr_group = {
  1355. .attrs = (struct attribute **)sidetone_attrs,
  1356. };
  1357. static int __devinit omap_st_add(struct omap_mcbsp *mcbsp)
  1358. {
  1359. struct platform_device *pdev;
  1360. struct resource *res;
  1361. struct omap_mcbsp_st_data *st_data;
  1362. int err;
  1363. st_data = kzalloc(sizeof(*mcbsp->st_data), GFP_KERNEL);
  1364. if (!st_data) {
  1365. err = -ENOMEM;
  1366. goto err1;
  1367. }
  1368. pdev = container_of(mcbsp->dev, struct platform_device, dev);
  1369. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sidetone");
  1370. st_data->io_base_st = ioremap(res->start, resource_size(res));
  1371. if (!st_data->io_base_st) {
  1372. err = -ENOMEM;
  1373. goto err2;
  1374. }
  1375. err = sysfs_create_group(&mcbsp->dev->kobj, &sidetone_attr_group);
  1376. if (err)
  1377. goto err3;
  1378. mcbsp->st_data = st_data;
  1379. return 0;
  1380. err3:
  1381. iounmap(st_data->io_base_st);
  1382. err2:
  1383. kfree(st_data);
  1384. err1:
  1385. return err;
  1386. }
  1387. static void __devexit omap_st_remove(struct omap_mcbsp *mcbsp)
  1388. {
  1389. struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
  1390. if (st_data) {
  1391. sysfs_remove_group(&mcbsp->dev->kobj, &sidetone_attr_group);
  1392. iounmap(st_data->io_base_st);
  1393. kfree(st_data);
  1394. }
  1395. }
  1396. static inline void __devinit omap34xx_device_init(struct omap_mcbsp *mcbsp)
  1397. {
  1398. mcbsp->dma_op_mode = MCBSP_DMA_MODE_ELEMENT;
  1399. if (cpu_is_omap34xx()) {
  1400. /*
  1401. * Initially configure the maximum thresholds to a safe value.
  1402. * The McBSP FIFO usage with these values should not go under
  1403. * 16 locations.
  1404. * If the whole FIFO without safety buffer is used, than there
  1405. * is a possibility that the DMA will be not able to push the
  1406. * new data on time, causing channel shifts in runtime.
  1407. */
  1408. mcbsp->max_tx_thres = max_thres(mcbsp) - 0x10;
  1409. mcbsp->max_rx_thres = max_thres(mcbsp) - 0x10;
  1410. /*
  1411. * REVISIT: Set dmap_op_mode to THRESHOLD as default
  1412. * for mcbsp2 instances.
  1413. */
  1414. if (omap_additional_add(mcbsp->dev))
  1415. dev_warn(mcbsp->dev,
  1416. "Unable to create additional controls\n");
  1417. if (mcbsp->id == 2 || mcbsp->id == 3)
  1418. if (omap_st_add(mcbsp))
  1419. dev_warn(mcbsp->dev,
  1420. "Unable to create sidetone controls\n");
  1421. } else {
  1422. mcbsp->max_tx_thres = -EINVAL;
  1423. mcbsp->max_rx_thres = -EINVAL;
  1424. }
  1425. }
  1426. static inline void __devexit omap34xx_device_exit(struct omap_mcbsp *mcbsp)
  1427. {
  1428. if (cpu_is_omap34xx()) {
  1429. omap_additional_remove(mcbsp->dev);
  1430. if (mcbsp->id == 2 || mcbsp->id == 3)
  1431. omap_st_remove(mcbsp);
  1432. }
  1433. }
  1434. #else
  1435. static inline void __devinit omap34xx_device_init(struct omap_mcbsp *mcbsp) {}
  1436. static inline void __devexit omap34xx_device_exit(struct omap_mcbsp *mcbsp) {}
  1437. #endif /* CONFIG_ARCH_OMAP3 */
  1438. /*
  1439. * McBSP1 and McBSP3 are directly mapped on 1610 and 1510.
  1440. * 730 has only 2 McBSP, and both of them are MPU peripherals.
  1441. */
  1442. static int __devinit omap_mcbsp_probe(struct platform_device *pdev)
  1443. {
  1444. struct omap_mcbsp_platform_data *pdata = pdev->dev.platform_data;
  1445. struct omap_mcbsp *mcbsp;
  1446. int id = pdev->id - 1;
  1447. struct resource *res;
  1448. int ret = 0;
  1449. if (!pdata) {
  1450. dev_err(&pdev->dev, "McBSP device initialized without"
  1451. "platform data\n");
  1452. ret = -EINVAL;
  1453. goto exit;
  1454. }
  1455. dev_dbg(&pdev->dev, "Initializing OMAP McBSP (%d).\n", pdev->id);
  1456. if (id >= omap_mcbsp_count) {
  1457. dev_err(&pdev->dev, "Invalid McBSP device id (%d)\n", id);
  1458. ret = -EINVAL;
  1459. goto exit;
  1460. }
  1461. mcbsp = kzalloc(sizeof(struct omap_mcbsp), GFP_KERNEL);
  1462. if (!mcbsp) {
  1463. ret = -ENOMEM;
  1464. goto exit;
  1465. }
  1466. spin_lock_init(&mcbsp->lock);
  1467. mcbsp->id = id + 1;
  1468. mcbsp->free = true;
  1469. mcbsp->dma_tx_lch = -1;
  1470. mcbsp->dma_rx_lch = -1;
  1471. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mpu");
  1472. if (!res) {
  1473. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1474. if (!res) {
  1475. dev_err(&pdev->dev, "%s:mcbsp%d has invalid memory"
  1476. "resource\n", __func__, pdev->id);
  1477. ret = -ENOMEM;
  1478. goto exit;
  1479. }
  1480. }
  1481. mcbsp->phys_base = res->start;
  1482. omap_mcbsp_cache_size = resource_size(res);
  1483. mcbsp->io_base = ioremap(res->start, resource_size(res));
  1484. if (!mcbsp->io_base) {
  1485. ret = -ENOMEM;
  1486. goto err_ioremap;
  1487. }
  1488. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dma");
  1489. if (!res)
  1490. mcbsp->phys_dma_base = mcbsp->phys_base;
  1491. else
  1492. mcbsp->phys_dma_base = res->start;
  1493. /* Default I/O is IRQ based */
  1494. mcbsp->io_type = OMAP_MCBSP_IRQ_IO;
  1495. mcbsp->tx_irq = platform_get_irq_byname(pdev, "tx");
  1496. mcbsp->rx_irq = platform_get_irq_byname(pdev, "rx");
  1497. /* From OMAP4 there will be a single irq line */
  1498. if (mcbsp->tx_irq == -ENXIO)
  1499. mcbsp->tx_irq = platform_get_irq(pdev, 0);
  1500. res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
  1501. if (!res) {
  1502. dev_err(&pdev->dev, "%s:mcbsp%d has invalid rx DMA channel\n",
  1503. __func__, pdev->id);
  1504. ret = -ENODEV;
  1505. goto err_res;
  1506. }
  1507. mcbsp->dma_rx_sync = res->start;
  1508. res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
  1509. if (!res) {
  1510. dev_err(&pdev->dev, "%s:mcbsp%d has invalid tx DMA channel\n",
  1511. __func__, pdev->id);
  1512. ret = -ENODEV;
  1513. goto err_res;
  1514. }
  1515. mcbsp->dma_tx_sync = res->start;
  1516. mcbsp->iclk = clk_get(&pdev->dev, "ick");
  1517. if (IS_ERR(mcbsp->iclk)) {
  1518. ret = PTR_ERR(mcbsp->iclk);
  1519. dev_err(&pdev->dev, "unable to get ick: %d\n", ret);
  1520. goto err_res;
  1521. }
  1522. mcbsp->fclk = clk_get(&pdev->dev, "fck");
  1523. if (IS_ERR(mcbsp->fclk)) {
  1524. ret = PTR_ERR(mcbsp->fclk);
  1525. dev_err(&pdev->dev, "unable to get fck: %d\n", ret);
  1526. goto err_fclk;
  1527. }
  1528. mcbsp->pdata = pdata;
  1529. mcbsp->dev = &pdev->dev;
  1530. mcbsp_ptr[id] = mcbsp;
  1531. platform_set_drvdata(pdev, mcbsp);
  1532. /* Initialize mcbsp properties for OMAP34XX if needed / applicable */
  1533. omap34xx_device_init(mcbsp);
  1534. return 0;
  1535. err_fclk:
  1536. clk_put(mcbsp->iclk);
  1537. err_res:
  1538. iounmap(mcbsp->io_base);
  1539. err_ioremap:
  1540. kfree(mcbsp);
  1541. exit:
  1542. return ret;
  1543. }
  1544. static int __devexit omap_mcbsp_remove(struct platform_device *pdev)
  1545. {
  1546. struct omap_mcbsp *mcbsp = platform_get_drvdata(pdev);
  1547. platform_set_drvdata(pdev, NULL);
  1548. if (mcbsp) {
  1549. if (mcbsp->pdata && mcbsp->pdata->ops &&
  1550. mcbsp->pdata->ops->free)
  1551. mcbsp->pdata->ops->free(mcbsp->id);
  1552. omap34xx_device_exit(mcbsp);
  1553. clk_put(mcbsp->fclk);
  1554. clk_put(mcbsp->iclk);
  1555. iounmap(mcbsp->io_base);
  1556. kfree(mcbsp);
  1557. }
  1558. return 0;
  1559. }
  1560. static struct platform_driver omap_mcbsp_driver = {
  1561. .probe = omap_mcbsp_probe,
  1562. .remove = __devexit_p(omap_mcbsp_remove),
  1563. .driver = {
  1564. .name = "omap-mcbsp",
  1565. },
  1566. };
  1567. int __init omap_mcbsp_init(void)
  1568. {
  1569. /* Register the McBSP driver */
  1570. return platform_driver_register(&omap_mcbsp_driver);
  1571. }