mcbsp.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394
  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. mcbsp->rx_word_length = (MCBSP_READ_CACHE(mcbsp, RCR1) >> 5) & 0x7;
  714. mcbsp->tx_word_length = (MCBSP_READ_CACHE(mcbsp, XCR1) >> 5) & 0x7;
  715. /* Only enable SRG, if McBSP is master */
  716. w = MCBSP_READ_CACHE(mcbsp, PCR0);
  717. if (w & (FSXM | FSRM | CLKXM | CLKRM))
  718. enable_srg = !((MCBSP_READ_CACHE(mcbsp, SPCR2) |
  719. MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1);
  720. if (enable_srg) {
  721. /* Start the sample generator */
  722. w = MCBSP_READ_CACHE(mcbsp, SPCR2);
  723. MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 6));
  724. }
  725. /* Enable transmitter and receiver */
  726. tx &= 1;
  727. w = MCBSP_READ_CACHE(mcbsp, SPCR2);
  728. MCBSP_WRITE(mcbsp, SPCR2, w | tx);
  729. rx &= 1;
  730. w = MCBSP_READ_CACHE(mcbsp, SPCR1);
  731. MCBSP_WRITE(mcbsp, SPCR1, w | rx);
  732. /*
  733. * Worst case: CLKSRG*2 = 8000khz: (1/8000) * 2 * 2 usec
  734. * REVISIT: 100us may give enough time for two CLKSRG, however
  735. * due to some unknown PM related, clock gating etc. reason it
  736. * is now at 500us.
  737. */
  738. udelay(500);
  739. if (enable_srg) {
  740. /* Start frame sync */
  741. w = MCBSP_READ_CACHE(mcbsp, SPCR2);
  742. MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 7));
  743. }
  744. if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
  745. /* Release the transmitter and receiver */
  746. w = MCBSP_READ_CACHE(mcbsp, XCCR);
  747. w &= ~(tx ? XDISABLE : 0);
  748. MCBSP_WRITE(mcbsp, XCCR, w);
  749. w = MCBSP_READ_CACHE(mcbsp, RCCR);
  750. w &= ~(rx ? RDISABLE : 0);
  751. MCBSP_WRITE(mcbsp, RCCR, w);
  752. }
  753. /* Dump McBSP Regs */
  754. omap_mcbsp_dump_reg(id);
  755. }
  756. EXPORT_SYMBOL(omap_mcbsp_start);
  757. void omap_mcbsp_stop(unsigned int id, int tx, int rx)
  758. {
  759. struct omap_mcbsp *mcbsp;
  760. int idle;
  761. u16 w;
  762. if (!omap_mcbsp_check_valid_id(id)) {
  763. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  764. return;
  765. }
  766. mcbsp = id_to_mcbsp_ptr(id);
  767. /* Reset transmitter */
  768. tx &= 1;
  769. if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
  770. w = MCBSP_READ_CACHE(mcbsp, XCCR);
  771. w |= (tx ? XDISABLE : 0);
  772. MCBSP_WRITE(mcbsp, XCCR, w);
  773. }
  774. w = MCBSP_READ_CACHE(mcbsp, SPCR2);
  775. MCBSP_WRITE(mcbsp, SPCR2, w & ~tx);
  776. /* Reset receiver */
  777. rx &= 1;
  778. if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
  779. w = MCBSP_READ_CACHE(mcbsp, RCCR);
  780. w |= (rx ? RDISABLE : 0);
  781. MCBSP_WRITE(mcbsp, RCCR, w);
  782. }
  783. w = MCBSP_READ_CACHE(mcbsp, SPCR1);
  784. MCBSP_WRITE(mcbsp, SPCR1, w & ~rx);
  785. idle = !((MCBSP_READ_CACHE(mcbsp, SPCR2) |
  786. MCBSP_READ_CACHE(mcbsp, SPCR1)) & 1);
  787. if (idle) {
  788. /* Reset the sample rate generator */
  789. w = MCBSP_READ_CACHE(mcbsp, SPCR2);
  790. MCBSP_WRITE(mcbsp, SPCR2, w & ~(1 << 6));
  791. }
  792. if (cpu_is_omap34xx())
  793. omap_st_stop(mcbsp);
  794. }
  795. EXPORT_SYMBOL(omap_mcbsp_stop);
  796. #ifdef CONFIG_ARCH_OMAP3
  797. #define max_thres(m) (mcbsp->pdata->buffer_size)
  798. #define valid_threshold(m, val) ((val) <= max_thres(m))
  799. #define THRESHOLD_PROP_BUILDER(prop) \
  800. static ssize_t prop##_show(struct device *dev, \
  801. struct device_attribute *attr, char *buf) \
  802. { \
  803. struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
  804. \
  805. return sprintf(buf, "%u\n", mcbsp->prop); \
  806. } \
  807. \
  808. static ssize_t prop##_store(struct device *dev, \
  809. struct device_attribute *attr, \
  810. const char *buf, size_t size) \
  811. { \
  812. struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
  813. unsigned long val; \
  814. int status; \
  815. \
  816. status = strict_strtoul(buf, 0, &val); \
  817. if (status) \
  818. return status; \
  819. \
  820. if (!valid_threshold(mcbsp, val)) \
  821. return -EDOM; \
  822. \
  823. mcbsp->prop = val; \
  824. return size; \
  825. } \
  826. \
  827. static DEVICE_ATTR(prop, 0644, prop##_show, prop##_store);
  828. THRESHOLD_PROP_BUILDER(max_tx_thres);
  829. THRESHOLD_PROP_BUILDER(max_rx_thres);
  830. static const char *dma_op_modes[] = {
  831. "element", "threshold", "frame",
  832. };
  833. static ssize_t dma_op_mode_show(struct device *dev,
  834. struct device_attribute *attr, char *buf)
  835. {
  836. struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
  837. int dma_op_mode, i = 0;
  838. ssize_t len = 0;
  839. const char * const *s;
  840. dma_op_mode = mcbsp->dma_op_mode;
  841. for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++) {
  842. if (dma_op_mode == i)
  843. len += sprintf(buf + len, "[%s] ", *s);
  844. else
  845. len += sprintf(buf + len, "%s ", *s);
  846. }
  847. len += sprintf(buf + len, "\n");
  848. return len;
  849. }
  850. static ssize_t dma_op_mode_store(struct device *dev,
  851. struct device_attribute *attr,
  852. const char *buf, size_t size)
  853. {
  854. struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
  855. const char * const *s;
  856. int i = 0;
  857. for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++)
  858. if (sysfs_streq(buf, *s))
  859. break;
  860. if (i == ARRAY_SIZE(dma_op_modes))
  861. return -EINVAL;
  862. spin_lock_irq(&mcbsp->lock);
  863. if (!mcbsp->free) {
  864. size = -EBUSY;
  865. goto unlock;
  866. }
  867. mcbsp->dma_op_mode = i;
  868. unlock:
  869. spin_unlock_irq(&mcbsp->lock);
  870. return size;
  871. }
  872. static DEVICE_ATTR(dma_op_mode, 0644, dma_op_mode_show, dma_op_mode_store);
  873. static ssize_t st_taps_show(struct device *dev,
  874. struct device_attribute *attr, char *buf)
  875. {
  876. struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
  877. struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
  878. ssize_t status = 0;
  879. int i;
  880. spin_lock_irq(&mcbsp->lock);
  881. for (i = 0; i < st_data->nr_taps; i++)
  882. status += sprintf(&buf[status], (i ? ", %d" : "%d"),
  883. st_data->taps[i]);
  884. if (i)
  885. status += sprintf(&buf[status], "\n");
  886. spin_unlock_irq(&mcbsp->lock);
  887. return status;
  888. }
  889. static ssize_t st_taps_store(struct device *dev,
  890. struct device_attribute *attr,
  891. const char *buf, size_t size)
  892. {
  893. struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
  894. struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
  895. int val, tmp, status, i = 0;
  896. spin_lock_irq(&mcbsp->lock);
  897. memset(st_data->taps, 0, sizeof(st_data->taps));
  898. st_data->nr_taps = 0;
  899. do {
  900. status = sscanf(buf, "%d%n", &val, &tmp);
  901. if (status < 0 || status == 0) {
  902. size = -EINVAL;
  903. goto out;
  904. }
  905. if (val < -32768 || val > 32767) {
  906. size = -EINVAL;
  907. goto out;
  908. }
  909. st_data->taps[i++] = val;
  910. buf += tmp;
  911. if (*buf != ',')
  912. break;
  913. buf++;
  914. } while (1);
  915. st_data->nr_taps = i;
  916. out:
  917. spin_unlock_irq(&mcbsp->lock);
  918. return size;
  919. }
  920. static DEVICE_ATTR(st_taps, 0644, st_taps_show, st_taps_store);
  921. static const struct attribute *additional_attrs[] = {
  922. &dev_attr_max_tx_thres.attr,
  923. &dev_attr_max_rx_thres.attr,
  924. &dev_attr_dma_op_mode.attr,
  925. NULL,
  926. };
  927. static const struct attribute_group additional_attr_group = {
  928. .attrs = (struct attribute **)additional_attrs,
  929. };
  930. static inline int __devinit omap_additional_add(struct device *dev)
  931. {
  932. return sysfs_create_group(&dev->kobj, &additional_attr_group);
  933. }
  934. static inline void __devexit omap_additional_remove(struct device *dev)
  935. {
  936. sysfs_remove_group(&dev->kobj, &additional_attr_group);
  937. }
  938. static const struct attribute *sidetone_attrs[] = {
  939. &dev_attr_st_taps.attr,
  940. NULL,
  941. };
  942. static const struct attribute_group sidetone_attr_group = {
  943. .attrs = (struct attribute **)sidetone_attrs,
  944. };
  945. static int __devinit omap_st_add(struct omap_mcbsp *mcbsp)
  946. {
  947. struct platform_device *pdev;
  948. struct resource *res;
  949. struct omap_mcbsp_st_data *st_data;
  950. int err;
  951. st_data = kzalloc(sizeof(*mcbsp->st_data), GFP_KERNEL);
  952. if (!st_data) {
  953. err = -ENOMEM;
  954. goto err1;
  955. }
  956. pdev = container_of(mcbsp->dev, struct platform_device, dev);
  957. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sidetone");
  958. st_data->io_base_st = ioremap(res->start, resource_size(res));
  959. if (!st_data->io_base_st) {
  960. err = -ENOMEM;
  961. goto err2;
  962. }
  963. err = sysfs_create_group(&mcbsp->dev->kobj, &sidetone_attr_group);
  964. if (err)
  965. goto err3;
  966. mcbsp->st_data = st_data;
  967. return 0;
  968. err3:
  969. iounmap(st_data->io_base_st);
  970. err2:
  971. kfree(st_data);
  972. err1:
  973. return err;
  974. }
  975. static void __devexit omap_st_remove(struct omap_mcbsp *mcbsp)
  976. {
  977. struct omap_mcbsp_st_data *st_data = mcbsp->st_data;
  978. if (st_data) {
  979. sysfs_remove_group(&mcbsp->dev->kobj, &sidetone_attr_group);
  980. iounmap(st_data->io_base_st);
  981. kfree(st_data);
  982. }
  983. }
  984. static inline void __devinit omap34xx_device_init(struct omap_mcbsp *mcbsp)
  985. {
  986. mcbsp->dma_op_mode = MCBSP_DMA_MODE_ELEMENT;
  987. if (cpu_is_omap34xx()) {
  988. /*
  989. * Initially configure the maximum thresholds to a safe value.
  990. * The McBSP FIFO usage with these values should not go under
  991. * 16 locations.
  992. * If the whole FIFO without safety buffer is used, than there
  993. * is a possibility that the DMA will be not able to push the
  994. * new data on time, causing channel shifts in runtime.
  995. */
  996. mcbsp->max_tx_thres = max_thres(mcbsp) - 0x10;
  997. mcbsp->max_rx_thres = max_thres(mcbsp) - 0x10;
  998. /*
  999. * REVISIT: Set dmap_op_mode to THRESHOLD as default
  1000. * for mcbsp2 instances.
  1001. */
  1002. if (omap_additional_add(mcbsp->dev))
  1003. dev_warn(mcbsp->dev,
  1004. "Unable to create additional controls\n");
  1005. if (mcbsp->id == 2 || mcbsp->id == 3)
  1006. if (omap_st_add(mcbsp))
  1007. dev_warn(mcbsp->dev,
  1008. "Unable to create sidetone controls\n");
  1009. } else {
  1010. mcbsp->max_tx_thres = -EINVAL;
  1011. mcbsp->max_rx_thres = -EINVAL;
  1012. }
  1013. }
  1014. static inline void __devexit omap34xx_device_exit(struct omap_mcbsp *mcbsp)
  1015. {
  1016. if (cpu_is_omap34xx()) {
  1017. omap_additional_remove(mcbsp->dev);
  1018. if (mcbsp->id == 2 || mcbsp->id == 3)
  1019. omap_st_remove(mcbsp);
  1020. }
  1021. }
  1022. #else
  1023. static inline void __devinit omap34xx_device_init(struct omap_mcbsp *mcbsp) {}
  1024. static inline void __devexit omap34xx_device_exit(struct omap_mcbsp *mcbsp) {}
  1025. #endif /* CONFIG_ARCH_OMAP3 */
  1026. /*
  1027. * McBSP1 and McBSP3 are directly mapped on 1610 and 1510.
  1028. * 730 has only 2 McBSP, and both of them are MPU peripherals.
  1029. */
  1030. static int __devinit omap_mcbsp_probe(struct platform_device *pdev)
  1031. {
  1032. struct omap_mcbsp_platform_data *pdata = pdev->dev.platform_data;
  1033. struct omap_mcbsp *mcbsp;
  1034. int id = pdev->id - 1;
  1035. struct resource *res;
  1036. int ret = 0;
  1037. if (!pdata) {
  1038. dev_err(&pdev->dev, "McBSP device initialized without"
  1039. "platform data\n");
  1040. ret = -EINVAL;
  1041. goto exit;
  1042. }
  1043. dev_dbg(&pdev->dev, "Initializing OMAP McBSP (%d).\n", pdev->id);
  1044. if (id >= omap_mcbsp_count) {
  1045. dev_err(&pdev->dev, "Invalid McBSP device id (%d)\n", id);
  1046. ret = -EINVAL;
  1047. goto exit;
  1048. }
  1049. mcbsp = kzalloc(sizeof(struct omap_mcbsp), GFP_KERNEL);
  1050. if (!mcbsp) {
  1051. ret = -ENOMEM;
  1052. goto exit;
  1053. }
  1054. spin_lock_init(&mcbsp->lock);
  1055. mcbsp->id = id + 1;
  1056. mcbsp->free = true;
  1057. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mpu");
  1058. if (!res) {
  1059. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1060. if (!res) {
  1061. dev_err(&pdev->dev, "%s:mcbsp%d has invalid memory"
  1062. "resource\n", __func__, pdev->id);
  1063. ret = -ENOMEM;
  1064. goto exit;
  1065. }
  1066. }
  1067. mcbsp->phys_base = res->start;
  1068. omap_mcbsp_cache_size = resource_size(res);
  1069. mcbsp->io_base = ioremap(res->start, resource_size(res));
  1070. if (!mcbsp->io_base) {
  1071. ret = -ENOMEM;
  1072. goto err_ioremap;
  1073. }
  1074. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dma");
  1075. if (!res)
  1076. mcbsp->phys_dma_base = mcbsp->phys_base;
  1077. else
  1078. mcbsp->phys_dma_base = res->start;
  1079. mcbsp->tx_irq = platform_get_irq_byname(pdev, "tx");
  1080. mcbsp->rx_irq = platform_get_irq_byname(pdev, "rx");
  1081. /* From OMAP4 there will be a single irq line */
  1082. if (mcbsp->tx_irq == -ENXIO)
  1083. mcbsp->tx_irq = platform_get_irq(pdev, 0);
  1084. res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
  1085. if (!res) {
  1086. dev_err(&pdev->dev, "%s:mcbsp%d has invalid rx DMA channel\n",
  1087. __func__, pdev->id);
  1088. ret = -ENODEV;
  1089. goto err_res;
  1090. }
  1091. mcbsp->dma_rx_sync = res->start;
  1092. res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
  1093. if (!res) {
  1094. dev_err(&pdev->dev, "%s:mcbsp%d has invalid tx DMA channel\n",
  1095. __func__, pdev->id);
  1096. ret = -ENODEV;
  1097. goto err_res;
  1098. }
  1099. mcbsp->dma_tx_sync = res->start;
  1100. mcbsp->fclk = clk_get(&pdev->dev, "fck");
  1101. if (IS_ERR(mcbsp->fclk)) {
  1102. ret = PTR_ERR(mcbsp->fclk);
  1103. dev_err(&pdev->dev, "unable to get fck: %d\n", ret);
  1104. goto err_res;
  1105. }
  1106. mcbsp->pdata = pdata;
  1107. mcbsp->dev = &pdev->dev;
  1108. mcbsp_ptr[id] = mcbsp;
  1109. mcbsp->mcbsp_config_type = pdata->mcbsp_config_type;
  1110. platform_set_drvdata(pdev, mcbsp);
  1111. pm_runtime_enable(mcbsp->dev);
  1112. /* Initialize mcbsp properties for OMAP34XX if needed / applicable */
  1113. omap34xx_device_init(mcbsp);
  1114. return 0;
  1115. err_res:
  1116. iounmap(mcbsp->io_base);
  1117. err_ioremap:
  1118. kfree(mcbsp);
  1119. exit:
  1120. return ret;
  1121. }
  1122. static int __devexit omap_mcbsp_remove(struct platform_device *pdev)
  1123. {
  1124. struct omap_mcbsp *mcbsp = platform_get_drvdata(pdev);
  1125. platform_set_drvdata(pdev, NULL);
  1126. if (mcbsp) {
  1127. if (mcbsp->pdata && mcbsp->pdata->ops &&
  1128. mcbsp->pdata->ops->free)
  1129. mcbsp->pdata->ops->free(mcbsp->id);
  1130. omap34xx_device_exit(mcbsp);
  1131. clk_put(mcbsp->fclk);
  1132. iounmap(mcbsp->io_base);
  1133. kfree(mcbsp);
  1134. }
  1135. return 0;
  1136. }
  1137. static struct platform_driver omap_mcbsp_driver = {
  1138. .probe = omap_mcbsp_probe,
  1139. .remove = __devexit_p(omap_mcbsp_remove),
  1140. .driver = {
  1141. .name = "omap-mcbsp",
  1142. },
  1143. };
  1144. int __init omap_mcbsp_init(void)
  1145. {
  1146. /* Register the McBSP driver */
  1147. return platform_driver_register(&omap_mcbsp_driver);
  1148. }