mcbsp.c 47 KB

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