mcbsp.c 34 KB

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