mcbsp.c 32 KB

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