mcbsp.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376
  1. /*
  2. * linux/arch/arm/plat-omap/mcbsp.c
  3. *
  4. * Copyright (C) 2004 Nokia Corporation
  5. * Author: Samuel Ortiz <samuel.ortiz@nokia.com>
  6. *
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * Multichannel mode not supported.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/device.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/wait.h>
  19. #include <linux/completion.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/err.h>
  22. #include <linux/clk.h>
  23. #include <linux/delay.h>
  24. #include <linux/io.h>
  25. #include <plat/dma.h>
  26. #include <plat/mcbsp.h>
  27. struct omap_mcbsp **mcbsp_ptr;
  28. int omap_mcbsp_count, omap_mcbsp_cache_size;
  29. void omap_mcbsp_write(struct omap_mcbsp *mcbsp, u16 reg, u32 val)
  30. {
  31. if (cpu_class_is_omap1()) {
  32. ((u16 *)mcbsp->reg_cache)[reg / sizeof(u16)] = (u16)val;
  33. __raw_writew((u16)val, mcbsp->io_base + reg);
  34. } else if (cpu_is_omap2420()) {
  35. ((u16 *)mcbsp->reg_cache)[reg / sizeof(u32)] = (u16)val;
  36. __raw_writew((u16)val, mcbsp->io_base + reg);
  37. } else {
  38. ((u32 *)mcbsp->reg_cache)[reg / sizeof(u32)] = val;
  39. __raw_writel(val, mcbsp->io_base + reg);
  40. }
  41. }
  42. int omap_mcbsp_read(struct omap_mcbsp *mcbsp, u16 reg, bool from_cache)
  43. {
  44. if (cpu_class_is_omap1()) {
  45. return !from_cache ? __raw_readw(mcbsp->io_base + reg) :
  46. ((u16 *)mcbsp->reg_cache)[reg / sizeof(u16)];
  47. } else if (cpu_is_omap2420()) {
  48. return !from_cache ? __raw_readw(mcbsp->io_base + reg) :
  49. ((u16 *)mcbsp->reg_cache)[reg / sizeof(u32)];
  50. } else {
  51. return !from_cache ? __raw_readl(mcbsp->io_base + reg) :
  52. ((u32 *)mcbsp->reg_cache)[reg / sizeof(u32)];
  53. }
  54. }
  55. #define MCBSP_READ(mcbsp, reg) \
  56. omap_mcbsp_read(mcbsp, OMAP_MCBSP_REG_##reg, 0)
  57. #define MCBSP_WRITE(mcbsp, reg, val) \
  58. omap_mcbsp_write(mcbsp, OMAP_MCBSP_REG_##reg, val)
  59. #define MCBSP_READ_CACHE(mcbsp, reg) \
  60. omap_mcbsp_read(mcbsp, OMAP_MCBSP_REG_##reg, 1)
  61. #define omap_mcbsp_check_valid_id(id) (id < omap_mcbsp_count)
  62. #define id_to_mcbsp_ptr(id) mcbsp_ptr[id];
  63. static void omap_mcbsp_dump_reg(u8 id)
  64. {
  65. struct omap_mcbsp *mcbsp = id_to_mcbsp_ptr(id);
  66. dev_dbg(mcbsp->dev, "**** McBSP%d regs ****\n", mcbsp->id);
  67. dev_dbg(mcbsp->dev, "DRR2: 0x%04x\n",
  68. MCBSP_READ(mcbsp, DRR2));
  69. dev_dbg(mcbsp->dev, "DRR1: 0x%04x\n",
  70. MCBSP_READ(mcbsp, DRR1));
  71. dev_dbg(mcbsp->dev, "DXR2: 0x%04x\n",
  72. MCBSP_READ(mcbsp, DXR2));
  73. dev_dbg(mcbsp->dev, "DXR1: 0x%04x\n",
  74. MCBSP_READ(mcbsp, DXR1));
  75. dev_dbg(mcbsp->dev, "SPCR2: 0x%04x\n",
  76. MCBSP_READ(mcbsp, SPCR2));
  77. dev_dbg(mcbsp->dev, "SPCR1: 0x%04x\n",
  78. MCBSP_READ(mcbsp, SPCR1));
  79. dev_dbg(mcbsp->dev, "RCR2: 0x%04x\n",
  80. MCBSP_READ(mcbsp, RCR2));
  81. dev_dbg(mcbsp->dev, "RCR1: 0x%04x\n",
  82. MCBSP_READ(mcbsp, RCR1));
  83. dev_dbg(mcbsp->dev, "XCR2: 0x%04x\n",
  84. MCBSP_READ(mcbsp, XCR2));
  85. dev_dbg(mcbsp->dev, "XCR1: 0x%04x\n",
  86. MCBSP_READ(mcbsp, XCR1));
  87. dev_dbg(mcbsp->dev, "SRGR2: 0x%04x\n",
  88. MCBSP_READ(mcbsp, SRGR2));
  89. dev_dbg(mcbsp->dev, "SRGR1: 0x%04x\n",
  90. MCBSP_READ(mcbsp, SRGR1));
  91. dev_dbg(mcbsp->dev, "PCR0: 0x%04x\n",
  92. MCBSP_READ(mcbsp, PCR0));
  93. dev_dbg(mcbsp->dev, "***********************\n");
  94. }
  95. static irqreturn_t omap_mcbsp_tx_irq_handler(int irq, void *dev_id)
  96. {
  97. struct omap_mcbsp *mcbsp_tx = dev_id;
  98. u16 irqst_spcr2;
  99. irqst_spcr2 = MCBSP_READ(mcbsp_tx, SPCR2);
  100. dev_dbg(mcbsp_tx->dev, "TX IRQ callback : 0x%x\n", irqst_spcr2);
  101. if (irqst_spcr2 & XSYNC_ERR) {
  102. dev_err(mcbsp_tx->dev, "TX Frame Sync Error! : 0x%x\n",
  103. irqst_spcr2);
  104. /* Writing zero to XSYNC_ERR clears the IRQ */
  105. MCBSP_WRITE(mcbsp_tx, SPCR2, irqst_spcr2 & ~(XSYNC_ERR));
  106. } else {
  107. complete(&mcbsp_tx->tx_irq_completion);
  108. }
  109. return IRQ_HANDLED;
  110. }
  111. static irqreturn_t omap_mcbsp_rx_irq_handler(int irq, void *dev_id)
  112. {
  113. struct omap_mcbsp *mcbsp_rx = dev_id;
  114. u16 irqst_spcr1;
  115. irqst_spcr1 = MCBSP_READ(mcbsp_rx, SPCR1);
  116. dev_dbg(mcbsp_rx->dev, "RX IRQ callback : 0x%x\n", irqst_spcr1);
  117. if (irqst_spcr1 & RSYNC_ERR) {
  118. dev_err(mcbsp_rx->dev, "RX Frame Sync Error! : 0x%x\n",
  119. irqst_spcr1);
  120. /* Writing zero to RSYNC_ERR clears the IRQ */
  121. MCBSP_WRITE(mcbsp_rx, SPCR1, irqst_spcr1 & ~(RSYNC_ERR));
  122. } else {
  123. complete(&mcbsp_rx->tx_irq_completion);
  124. }
  125. return IRQ_HANDLED;
  126. }
  127. static void omap_mcbsp_tx_dma_callback(int lch, u16 ch_status, void *data)
  128. {
  129. struct omap_mcbsp *mcbsp_dma_tx = data;
  130. dev_dbg(mcbsp_dma_tx->dev, "TX DMA callback : 0x%x\n",
  131. MCBSP_READ(mcbsp_dma_tx, SPCR2));
  132. /* We can free the channels */
  133. omap_free_dma(mcbsp_dma_tx->dma_tx_lch);
  134. mcbsp_dma_tx->dma_tx_lch = -1;
  135. complete(&mcbsp_dma_tx->tx_dma_completion);
  136. }
  137. static void omap_mcbsp_rx_dma_callback(int lch, u16 ch_status, void *data)
  138. {
  139. struct omap_mcbsp *mcbsp_dma_rx = data;
  140. dev_dbg(mcbsp_dma_rx->dev, "RX DMA callback : 0x%x\n",
  141. MCBSP_READ(mcbsp_dma_rx, SPCR2));
  142. /* We can free the channels */
  143. omap_free_dma(mcbsp_dma_rx->dma_rx_lch);
  144. mcbsp_dma_rx->dma_rx_lch = -1;
  145. complete(&mcbsp_dma_rx->rx_dma_completion);
  146. }
  147. /*
  148. * omap_mcbsp_config simply write a config to the
  149. * appropriate McBSP.
  150. * You either call this function or set the McBSP registers
  151. * by yourself before calling omap_mcbsp_start().
  152. */
  153. void omap_mcbsp_config(unsigned int id, const struct omap_mcbsp_reg_cfg *config)
  154. {
  155. struct omap_mcbsp *mcbsp;
  156. if (!omap_mcbsp_check_valid_id(id)) {
  157. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  158. return;
  159. }
  160. mcbsp = id_to_mcbsp_ptr(id);
  161. dev_dbg(mcbsp->dev, "Configuring McBSP%d phys_base: 0x%08lx\n",
  162. mcbsp->id, mcbsp->phys_base);
  163. /* We write the given config */
  164. MCBSP_WRITE(mcbsp, SPCR2, config->spcr2);
  165. MCBSP_WRITE(mcbsp, SPCR1, config->spcr1);
  166. MCBSP_WRITE(mcbsp, RCR2, config->rcr2);
  167. MCBSP_WRITE(mcbsp, RCR1, config->rcr1);
  168. MCBSP_WRITE(mcbsp, XCR2, config->xcr2);
  169. MCBSP_WRITE(mcbsp, XCR1, config->xcr1);
  170. MCBSP_WRITE(mcbsp, SRGR2, config->srgr2);
  171. MCBSP_WRITE(mcbsp, SRGR1, config->srgr1);
  172. MCBSP_WRITE(mcbsp, MCR2, config->mcr2);
  173. MCBSP_WRITE(mcbsp, MCR1, config->mcr1);
  174. MCBSP_WRITE(mcbsp, PCR0, config->pcr0);
  175. if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx()) {
  176. MCBSP_WRITE(mcbsp, XCCR, config->xccr);
  177. MCBSP_WRITE(mcbsp, RCCR, config->rccr);
  178. }
  179. }
  180. EXPORT_SYMBOL(omap_mcbsp_config);
  181. #ifdef CONFIG_ARCH_OMAP3
  182. /*
  183. * omap_mcbsp_set_tx_threshold configures how to deal
  184. * with transmit threshold. the threshold value and handler can be
  185. * configure in here.
  186. */
  187. void omap_mcbsp_set_tx_threshold(unsigned int id, u16 threshold)
  188. {
  189. struct omap_mcbsp *mcbsp;
  190. if (!cpu_is_omap34xx())
  191. return;
  192. if (!omap_mcbsp_check_valid_id(id)) {
  193. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  194. return;
  195. }
  196. mcbsp = id_to_mcbsp_ptr(id);
  197. MCBSP_WRITE(mcbsp, THRSH2, threshold);
  198. }
  199. EXPORT_SYMBOL(omap_mcbsp_set_tx_threshold);
  200. /*
  201. * omap_mcbsp_set_rx_threshold configures how to deal
  202. * with receive threshold. the threshold value and handler can be
  203. * configure in here.
  204. */
  205. void omap_mcbsp_set_rx_threshold(unsigned int id, u16 threshold)
  206. {
  207. struct omap_mcbsp *mcbsp;
  208. if (!cpu_is_omap34xx())
  209. return;
  210. if (!omap_mcbsp_check_valid_id(id)) {
  211. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  212. return;
  213. }
  214. mcbsp = id_to_mcbsp_ptr(id);
  215. MCBSP_WRITE(mcbsp, THRSH1, threshold);
  216. }
  217. EXPORT_SYMBOL(omap_mcbsp_set_rx_threshold);
  218. /*
  219. * omap_mcbsp_get_max_tx_thres just return the current configured
  220. * maximum threshold for transmission
  221. */
  222. u16 omap_mcbsp_get_max_tx_threshold(unsigned int id)
  223. {
  224. struct omap_mcbsp *mcbsp;
  225. if (!omap_mcbsp_check_valid_id(id)) {
  226. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  227. return -ENODEV;
  228. }
  229. mcbsp = id_to_mcbsp_ptr(id);
  230. return mcbsp->max_tx_thres;
  231. }
  232. EXPORT_SYMBOL(omap_mcbsp_get_max_tx_threshold);
  233. /*
  234. * omap_mcbsp_get_max_rx_thres just return the current configured
  235. * maximum threshold for reception
  236. */
  237. u16 omap_mcbsp_get_max_rx_threshold(unsigned int id)
  238. {
  239. struct omap_mcbsp *mcbsp;
  240. if (!omap_mcbsp_check_valid_id(id)) {
  241. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  242. return -ENODEV;
  243. }
  244. mcbsp = id_to_mcbsp_ptr(id);
  245. return mcbsp->max_rx_thres;
  246. }
  247. EXPORT_SYMBOL(omap_mcbsp_get_max_rx_threshold);
  248. /*
  249. * omap_mcbsp_get_dma_op_mode just return the current configured
  250. * operating mode for the mcbsp channel
  251. */
  252. int omap_mcbsp_get_dma_op_mode(unsigned int id)
  253. {
  254. struct omap_mcbsp *mcbsp;
  255. int dma_op_mode;
  256. if (!omap_mcbsp_check_valid_id(id)) {
  257. printk(KERN_ERR "%s: Invalid id (%u)\n", __func__, id + 1);
  258. return -ENODEV;
  259. }
  260. mcbsp = id_to_mcbsp_ptr(id);
  261. dma_op_mode = mcbsp->dma_op_mode;
  262. return dma_op_mode;
  263. }
  264. EXPORT_SYMBOL(omap_mcbsp_get_dma_op_mode);
  265. static inline void omap34xx_mcbsp_request(struct omap_mcbsp *mcbsp)
  266. {
  267. /*
  268. * Enable wakup behavior, smart idle and all wakeups
  269. * REVISIT: some wakeups may be unnecessary
  270. */
  271. if (cpu_is_omap34xx()) {
  272. u16 syscon;
  273. syscon = MCBSP_READ(mcbsp, SYSCON);
  274. syscon &= ~(ENAWAKEUP | SIDLEMODE(0x03) | CLOCKACTIVITY(0x03));
  275. if (mcbsp->dma_op_mode == MCBSP_DMA_MODE_THRESHOLD) {
  276. syscon |= (ENAWAKEUP | SIDLEMODE(0x02) |
  277. CLOCKACTIVITY(0x02));
  278. MCBSP_WRITE(mcbsp, WAKEUPEN, XRDYEN | RRDYEN);
  279. } else {
  280. syscon |= SIDLEMODE(0x01);
  281. }
  282. MCBSP_WRITE(mcbsp, SYSCON, syscon);
  283. }
  284. }
  285. static inline void omap34xx_mcbsp_free(struct omap_mcbsp *mcbsp)
  286. {
  287. /*
  288. * Disable wakup behavior, smart idle and all wakeups
  289. */
  290. if (cpu_is_omap34xx()) {
  291. u16 syscon;
  292. syscon = MCBSP_READ(mcbsp, SYSCON);
  293. syscon &= ~(ENAWAKEUP | SIDLEMODE(0x03) | CLOCKACTIVITY(0x03));
  294. /*
  295. * HW bug workaround - If no_idle mode is taken, we need to
  296. * go to smart_idle before going to always_idle, or the
  297. * device will not hit retention anymore.
  298. */
  299. syscon |= SIDLEMODE(0x02);
  300. MCBSP_WRITE(mcbsp, SYSCON, syscon);
  301. syscon &= ~(SIDLEMODE(0x03));
  302. MCBSP_WRITE(mcbsp, SYSCON, syscon);
  303. MCBSP_WRITE(mcbsp, WAKEUPEN, 0);
  304. }
  305. }
  306. #else
  307. static inline void omap34xx_mcbsp_request(struct omap_mcbsp *mcbsp) {}
  308. static inline void omap34xx_mcbsp_free(struct omap_mcbsp *mcbsp) {}
  309. #endif
  310. /*
  311. * We can choose between IRQ based or polled IO.
  312. * This needs to be called before omap_mcbsp_request().
  313. */
  314. int omap_mcbsp_set_io_type(unsigned int id, omap_mcbsp_io_type_t io_type)
  315. {
  316. struct omap_mcbsp *mcbsp;
  317. if (!omap_mcbsp_check_valid_id(id)) {
  318. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  319. return -ENODEV;
  320. }
  321. mcbsp = id_to_mcbsp_ptr(id);
  322. spin_lock(&mcbsp->lock);
  323. if (!mcbsp->free) {
  324. dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
  325. mcbsp->id);
  326. spin_unlock(&mcbsp->lock);
  327. return -EINVAL;
  328. }
  329. mcbsp->io_type = io_type;
  330. spin_unlock(&mcbsp->lock);
  331. return 0;
  332. }
  333. EXPORT_SYMBOL(omap_mcbsp_set_io_type);
  334. int omap_mcbsp_request(unsigned int id)
  335. {
  336. struct omap_mcbsp *mcbsp;
  337. void *reg_cache;
  338. int err;
  339. if (!omap_mcbsp_check_valid_id(id)) {
  340. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  341. return -ENODEV;
  342. }
  343. mcbsp = id_to_mcbsp_ptr(id);
  344. reg_cache = kzalloc(omap_mcbsp_cache_size, GFP_KERNEL);
  345. if (!reg_cache) {
  346. return -ENOMEM;
  347. }
  348. spin_lock(&mcbsp->lock);
  349. if (!mcbsp->free) {
  350. dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
  351. mcbsp->id);
  352. err = -EBUSY;
  353. goto err_kfree;
  354. }
  355. mcbsp->free = 0;
  356. mcbsp->reg_cache = reg_cache;
  357. spin_unlock(&mcbsp->lock);
  358. if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->request)
  359. mcbsp->pdata->ops->request(id);
  360. clk_enable(mcbsp->iclk);
  361. clk_enable(mcbsp->fclk);
  362. /* Do procedure specific to omap34xx arch, if applicable */
  363. omap34xx_mcbsp_request(mcbsp);
  364. /*
  365. * Make sure that transmitter, receiver and sample-rate generator are
  366. * not running before activating IRQs.
  367. */
  368. MCBSP_WRITE(mcbsp, SPCR1, 0);
  369. MCBSP_WRITE(mcbsp, SPCR2, 0);
  370. if (mcbsp->io_type == OMAP_MCBSP_IRQ_IO) {
  371. /* We need to get IRQs here */
  372. init_completion(&mcbsp->tx_irq_completion);
  373. err = request_irq(mcbsp->tx_irq, omap_mcbsp_tx_irq_handler,
  374. 0, "McBSP", (void *)mcbsp);
  375. if (err != 0) {
  376. dev_err(mcbsp->dev, "Unable to request TX IRQ %d "
  377. "for McBSP%d\n", mcbsp->tx_irq,
  378. mcbsp->id);
  379. goto err_clk_disable;
  380. }
  381. init_completion(&mcbsp->rx_irq_completion);
  382. err = request_irq(mcbsp->rx_irq, omap_mcbsp_rx_irq_handler,
  383. 0, "McBSP", (void *)mcbsp);
  384. if (err != 0) {
  385. dev_err(mcbsp->dev, "Unable to request RX IRQ %d "
  386. "for McBSP%d\n", mcbsp->rx_irq,
  387. mcbsp->id);
  388. goto err_free_irq;
  389. }
  390. }
  391. return 0;
  392. err_free_irq:
  393. free_irq(mcbsp->tx_irq, (void *)mcbsp);
  394. err_clk_disable:
  395. if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
  396. mcbsp->pdata->ops->free(id);
  397. /* Do procedure specific to omap34xx arch, if applicable */
  398. omap34xx_mcbsp_free(mcbsp);
  399. clk_disable(mcbsp->fclk);
  400. clk_disable(mcbsp->iclk);
  401. spin_lock(&mcbsp->lock);
  402. mcbsp->free = 1;
  403. mcbsp->reg_cache = NULL;
  404. err_kfree:
  405. spin_unlock(&mcbsp->lock);
  406. kfree(reg_cache);
  407. return err;
  408. }
  409. EXPORT_SYMBOL(omap_mcbsp_request);
  410. void omap_mcbsp_free(unsigned int id)
  411. {
  412. struct omap_mcbsp *mcbsp;
  413. void *reg_cache;
  414. if (!omap_mcbsp_check_valid_id(id)) {
  415. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  416. return;
  417. }
  418. mcbsp = id_to_mcbsp_ptr(id);
  419. if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
  420. mcbsp->pdata->ops->free(id);
  421. /* Do procedure specific to omap34xx arch, if applicable */
  422. omap34xx_mcbsp_free(mcbsp);
  423. clk_disable(mcbsp->fclk);
  424. clk_disable(mcbsp->iclk);
  425. if (mcbsp->io_type == OMAP_MCBSP_IRQ_IO) {
  426. /* Free IRQs */
  427. free_irq(mcbsp->rx_irq, (void *)mcbsp);
  428. free_irq(mcbsp->tx_irq, (void *)mcbsp);
  429. }
  430. reg_cache = mcbsp->reg_cache;
  431. spin_lock(&mcbsp->lock);
  432. if (mcbsp->free)
  433. dev_err(mcbsp->dev, "McBSP%d was not reserved\n", mcbsp->id);
  434. else
  435. mcbsp->free = 1;
  436. mcbsp->reg_cache = NULL;
  437. spin_unlock(&mcbsp->lock);
  438. if (reg_cache)
  439. kfree(reg_cache);
  440. }
  441. EXPORT_SYMBOL(omap_mcbsp_free);
  442. /*
  443. * Here we start the McBSP, by enabling transmitter, receiver or both.
  444. * If no transmitter or receiver is active prior calling, then sample-rate
  445. * generator and frame sync are started.
  446. */
  447. void omap_mcbsp_start(unsigned int id, int tx, int rx)
  448. {
  449. struct omap_mcbsp *mcbsp;
  450. int idle;
  451. u16 w;
  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. mcbsp->rx_word_length = (MCBSP_READ(mcbsp, RCR1) >> 5) & 0x7;
  458. mcbsp->tx_word_length = (MCBSP_READ(mcbsp, XCR1) >> 5) & 0x7;
  459. idle = !((MCBSP_READ(mcbsp, SPCR2) | MCBSP_READ(mcbsp, SPCR1)) & 1);
  460. if (idle) {
  461. /* Start the sample generator */
  462. w = MCBSP_READ(mcbsp, SPCR2);
  463. MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 6));
  464. }
  465. /* Enable transmitter and receiver */
  466. tx &= 1;
  467. w = MCBSP_READ(mcbsp, SPCR2);
  468. MCBSP_WRITE(mcbsp, SPCR2, w | tx);
  469. rx &= 1;
  470. w = MCBSP_READ(mcbsp, SPCR1);
  471. MCBSP_WRITE(mcbsp, SPCR1, w | rx);
  472. /*
  473. * Worst case: CLKSRG*2 = 8000khz: (1/8000) * 2 * 2 usec
  474. * REVISIT: 100us may give enough time for two CLKSRG, however
  475. * due to some unknown PM related, clock gating etc. reason it
  476. * is now at 500us.
  477. */
  478. udelay(500);
  479. if (idle) {
  480. /* Start frame sync */
  481. w = MCBSP_READ(mcbsp, SPCR2);
  482. MCBSP_WRITE(mcbsp, SPCR2, w | (1 << 7));
  483. }
  484. if (cpu_is_omap2430() || cpu_is_omap34xx()) {
  485. /* Release the transmitter and receiver */
  486. w = MCBSP_READ(mcbsp, XCCR);
  487. w &= ~(tx ? XDISABLE : 0);
  488. MCBSP_WRITE(mcbsp, XCCR, w);
  489. w = MCBSP_READ(mcbsp, RCCR);
  490. w &= ~(rx ? RDISABLE : 0);
  491. MCBSP_WRITE(mcbsp, RCCR, w);
  492. }
  493. /* Dump McBSP Regs */
  494. omap_mcbsp_dump_reg(id);
  495. }
  496. EXPORT_SYMBOL(omap_mcbsp_start);
  497. void omap_mcbsp_stop(unsigned int id, int tx, int rx)
  498. {
  499. struct omap_mcbsp *mcbsp;
  500. int idle;
  501. u16 w;
  502. if (!omap_mcbsp_check_valid_id(id)) {
  503. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  504. return;
  505. }
  506. mcbsp = id_to_mcbsp_ptr(id);
  507. /* Reset transmitter */
  508. tx &= 1;
  509. if (cpu_is_omap2430() || cpu_is_omap34xx()) {
  510. w = MCBSP_READ(mcbsp, XCCR);
  511. w |= (tx ? XDISABLE : 0);
  512. MCBSP_WRITE(mcbsp, XCCR, w);
  513. }
  514. w = MCBSP_READ(mcbsp, SPCR2);
  515. MCBSP_WRITE(mcbsp, SPCR2, w & ~tx);
  516. /* Reset receiver */
  517. rx &= 1;
  518. if (cpu_is_omap2430() || cpu_is_omap34xx()) {
  519. w = MCBSP_READ(mcbsp, RCCR);
  520. w |= (rx ? RDISABLE : 0);
  521. MCBSP_WRITE(mcbsp, RCCR, w);
  522. }
  523. w = MCBSP_READ(mcbsp, SPCR1);
  524. MCBSP_WRITE(mcbsp, SPCR1, w & ~rx);
  525. idle = !((MCBSP_READ(mcbsp, SPCR2) | MCBSP_READ(mcbsp, SPCR1)) & 1);
  526. if (idle) {
  527. /* Reset the sample rate generator */
  528. w = MCBSP_READ(mcbsp, SPCR2);
  529. MCBSP_WRITE(mcbsp, SPCR2, w & ~(1 << 6));
  530. }
  531. }
  532. EXPORT_SYMBOL(omap_mcbsp_stop);
  533. /* polled mcbsp i/o operations */
  534. int omap_mcbsp_pollwrite(unsigned int id, u16 buf)
  535. {
  536. struct omap_mcbsp *mcbsp;
  537. if (!omap_mcbsp_check_valid_id(id)) {
  538. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  539. return -ENODEV;
  540. }
  541. mcbsp = id_to_mcbsp_ptr(id);
  542. MCBSP_WRITE(mcbsp, DXR1, buf);
  543. /* if frame sync error - clear the error */
  544. if (MCBSP_READ(mcbsp, SPCR2) & XSYNC_ERR) {
  545. /* clear error */
  546. MCBSP_WRITE(mcbsp, SPCR2,
  547. MCBSP_READ(mcbsp, SPCR2) & (~XSYNC_ERR));
  548. /* resend */
  549. return -1;
  550. } else {
  551. /* wait for transmit confirmation */
  552. int attemps = 0;
  553. while (!(MCBSP_READ(mcbsp, SPCR2) & XRDY)) {
  554. if (attemps++ > 1000) {
  555. MCBSP_WRITE(mcbsp, SPCR2,
  556. MCBSP_READ(mcbsp, SPCR2) & (~XRST));
  557. udelay(10);
  558. MCBSP_WRITE(mcbsp, SPCR2,
  559. MCBSP_READ(mcbsp, SPCR2) | (XRST));
  560. udelay(10);
  561. dev_err(mcbsp->dev, "Could not write to"
  562. " McBSP%d Register\n", mcbsp->id);
  563. return -2;
  564. }
  565. }
  566. }
  567. return 0;
  568. }
  569. EXPORT_SYMBOL(omap_mcbsp_pollwrite);
  570. int omap_mcbsp_pollread(unsigned int id, u16 *buf)
  571. {
  572. struct omap_mcbsp *mcbsp;
  573. if (!omap_mcbsp_check_valid_id(id)) {
  574. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  575. return -ENODEV;
  576. }
  577. mcbsp = id_to_mcbsp_ptr(id);
  578. /* if frame sync error - clear the error */
  579. if (MCBSP_READ(mcbsp, SPCR1) & RSYNC_ERR) {
  580. /* clear error */
  581. MCBSP_WRITE(mcbsp, SPCR1,
  582. MCBSP_READ(mcbsp, SPCR1) & (~RSYNC_ERR));
  583. /* resend */
  584. return -1;
  585. } else {
  586. /* wait for recieve confirmation */
  587. int attemps = 0;
  588. while (!(MCBSP_READ(mcbsp, SPCR1) & RRDY)) {
  589. if (attemps++ > 1000) {
  590. MCBSP_WRITE(mcbsp, SPCR1,
  591. MCBSP_READ(mcbsp, SPCR1) & (~RRST));
  592. udelay(10);
  593. MCBSP_WRITE(mcbsp, SPCR1,
  594. MCBSP_READ(mcbsp, SPCR1) | (RRST));
  595. udelay(10);
  596. dev_err(mcbsp->dev, "Could not read from"
  597. " McBSP%d Register\n", mcbsp->id);
  598. return -2;
  599. }
  600. }
  601. }
  602. *buf = MCBSP_READ(mcbsp, DRR1);
  603. return 0;
  604. }
  605. EXPORT_SYMBOL(omap_mcbsp_pollread);
  606. /*
  607. * IRQ based word transmission.
  608. */
  609. void omap_mcbsp_xmit_word(unsigned int id, u32 word)
  610. {
  611. struct omap_mcbsp *mcbsp;
  612. omap_mcbsp_word_length word_length;
  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. word_length = mcbsp->tx_word_length;
  619. wait_for_completion(&mcbsp->tx_irq_completion);
  620. if (word_length > OMAP_MCBSP_WORD_16)
  621. MCBSP_WRITE(mcbsp, DXR2, word >> 16);
  622. MCBSP_WRITE(mcbsp, DXR1, word & 0xffff);
  623. }
  624. EXPORT_SYMBOL(omap_mcbsp_xmit_word);
  625. u32 omap_mcbsp_recv_word(unsigned int id)
  626. {
  627. struct omap_mcbsp *mcbsp;
  628. u16 word_lsb, word_msb = 0;
  629. omap_mcbsp_word_length word_length;
  630. if (!omap_mcbsp_check_valid_id(id)) {
  631. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  632. return -ENODEV;
  633. }
  634. mcbsp = id_to_mcbsp_ptr(id);
  635. word_length = mcbsp->rx_word_length;
  636. wait_for_completion(&mcbsp->rx_irq_completion);
  637. if (word_length > OMAP_MCBSP_WORD_16)
  638. word_msb = MCBSP_READ(mcbsp, DRR2);
  639. word_lsb = MCBSP_READ(mcbsp, DRR1);
  640. return (word_lsb | (word_msb << 16));
  641. }
  642. EXPORT_SYMBOL(omap_mcbsp_recv_word);
  643. int omap_mcbsp_spi_master_xmit_word_poll(unsigned int id, u32 word)
  644. {
  645. struct omap_mcbsp *mcbsp;
  646. omap_mcbsp_word_length tx_word_length;
  647. omap_mcbsp_word_length rx_word_length;
  648. u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
  649. if (!omap_mcbsp_check_valid_id(id)) {
  650. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  651. return -ENODEV;
  652. }
  653. mcbsp = id_to_mcbsp_ptr(id);
  654. tx_word_length = mcbsp->tx_word_length;
  655. rx_word_length = mcbsp->rx_word_length;
  656. if (tx_word_length != rx_word_length)
  657. return -EINVAL;
  658. /* First we wait for the transmitter to be ready */
  659. spcr2 = MCBSP_READ(mcbsp, SPCR2);
  660. while (!(spcr2 & XRDY)) {
  661. spcr2 = MCBSP_READ(mcbsp, SPCR2);
  662. if (attempts++ > 1000) {
  663. /* We must reset the transmitter */
  664. MCBSP_WRITE(mcbsp, SPCR2, spcr2 & (~XRST));
  665. udelay(10);
  666. MCBSP_WRITE(mcbsp, SPCR2, spcr2 | XRST);
  667. udelay(10);
  668. dev_err(mcbsp->dev, "McBSP%d transmitter not "
  669. "ready\n", mcbsp->id);
  670. return -EAGAIN;
  671. }
  672. }
  673. /* Now we can push the data */
  674. if (tx_word_length > OMAP_MCBSP_WORD_16)
  675. MCBSP_WRITE(mcbsp, DXR2, word >> 16);
  676. MCBSP_WRITE(mcbsp, DXR1, word & 0xffff);
  677. /* We wait for the receiver to be ready */
  678. spcr1 = MCBSP_READ(mcbsp, SPCR1);
  679. while (!(spcr1 & RRDY)) {
  680. spcr1 = MCBSP_READ(mcbsp, SPCR1);
  681. if (attempts++ > 1000) {
  682. /* We must reset the receiver */
  683. MCBSP_WRITE(mcbsp, SPCR1, spcr1 & (~RRST));
  684. udelay(10);
  685. MCBSP_WRITE(mcbsp, SPCR1, spcr1 | RRST);
  686. udelay(10);
  687. dev_err(mcbsp->dev, "McBSP%d receiver not "
  688. "ready\n", mcbsp->id);
  689. return -EAGAIN;
  690. }
  691. }
  692. /* Receiver is ready, let's read the dummy data */
  693. if (rx_word_length > OMAP_MCBSP_WORD_16)
  694. word_msb = MCBSP_READ(mcbsp, DRR2);
  695. word_lsb = MCBSP_READ(mcbsp, DRR1);
  696. return 0;
  697. }
  698. EXPORT_SYMBOL(omap_mcbsp_spi_master_xmit_word_poll);
  699. int omap_mcbsp_spi_master_recv_word_poll(unsigned int id, u32 *word)
  700. {
  701. struct omap_mcbsp *mcbsp;
  702. u32 clock_word = 0;
  703. omap_mcbsp_word_length tx_word_length;
  704. omap_mcbsp_word_length rx_word_length;
  705. u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
  706. if (!omap_mcbsp_check_valid_id(id)) {
  707. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  708. return -ENODEV;
  709. }
  710. mcbsp = id_to_mcbsp_ptr(id);
  711. tx_word_length = mcbsp->tx_word_length;
  712. rx_word_length = mcbsp->rx_word_length;
  713. if (tx_word_length != rx_word_length)
  714. return -EINVAL;
  715. /* First we wait for the transmitter to be ready */
  716. spcr2 = MCBSP_READ(mcbsp, SPCR2);
  717. while (!(spcr2 & XRDY)) {
  718. spcr2 = MCBSP_READ(mcbsp, SPCR2);
  719. if (attempts++ > 1000) {
  720. /* We must reset the transmitter */
  721. MCBSP_WRITE(mcbsp, SPCR2, spcr2 & (~XRST));
  722. udelay(10);
  723. MCBSP_WRITE(mcbsp, SPCR2, spcr2 | XRST);
  724. udelay(10);
  725. dev_err(mcbsp->dev, "McBSP%d transmitter not "
  726. "ready\n", mcbsp->id);
  727. return -EAGAIN;
  728. }
  729. }
  730. /* We first need to enable the bus clock */
  731. if (tx_word_length > OMAP_MCBSP_WORD_16)
  732. MCBSP_WRITE(mcbsp, DXR2, clock_word >> 16);
  733. MCBSP_WRITE(mcbsp, DXR1, clock_word & 0xffff);
  734. /* We wait for the receiver to be ready */
  735. spcr1 = MCBSP_READ(mcbsp, SPCR1);
  736. while (!(spcr1 & RRDY)) {
  737. spcr1 = MCBSP_READ(mcbsp, SPCR1);
  738. if (attempts++ > 1000) {
  739. /* We must reset the receiver */
  740. MCBSP_WRITE(mcbsp, SPCR1, spcr1 & (~RRST));
  741. udelay(10);
  742. MCBSP_WRITE(mcbsp, SPCR1, spcr1 | RRST);
  743. udelay(10);
  744. dev_err(mcbsp->dev, "McBSP%d receiver not "
  745. "ready\n", mcbsp->id);
  746. return -EAGAIN;
  747. }
  748. }
  749. /* Receiver is ready, there is something for us */
  750. if (rx_word_length > OMAP_MCBSP_WORD_16)
  751. word_msb = MCBSP_READ(mcbsp, DRR2);
  752. word_lsb = MCBSP_READ(mcbsp, DRR1);
  753. word[0] = (word_lsb | (word_msb << 16));
  754. return 0;
  755. }
  756. EXPORT_SYMBOL(omap_mcbsp_spi_master_recv_word_poll);
  757. /*
  758. * Simple DMA based buffer rx/tx routines.
  759. * Nothing fancy, just a single buffer tx/rx through DMA.
  760. * The DMA resources are released once the transfer is done.
  761. * For anything fancier, you should use your own customized DMA
  762. * routines and callbacks.
  763. */
  764. int omap_mcbsp_xmit_buffer(unsigned int id, dma_addr_t buffer,
  765. unsigned int length)
  766. {
  767. struct omap_mcbsp *mcbsp;
  768. int dma_tx_ch;
  769. int src_port = 0;
  770. int dest_port = 0;
  771. int sync_dev = 0;
  772. if (!omap_mcbsp_check_valid_id(id)) {
  773. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  774. return -ENODEV;
  775. }
  776. mcbsp = id_to_mcbsp_ptr(id);
  777. if (omap_request_dma(mcbsp->dma_tx_sync, "McBSP TX",
  778. omap_mcbsp_tx_dma_callback,
  779. mcbsp,
  780. &dma_tx_ch)) {
  781. dev_err(mcbsp->dev, " Unable to request DMA channel for "
  782. "McBSP%d TX. Trying IRQ based TX\n",
  783. mcbsp->id);
  784. return -EAGAIN;
  785. }
  786. mcbsp->dma_tx_lch = dma_tx_ch;
  787. dev_err(mcbsp->dev, "McBSP%d TX DMA on channel %d\n", mcbsp->id,
  788. dma_tx_ch);
  789. init_completion(&mcbsp->tx_dma_completion);
  790. if (cpu_class_is_omap1()) {
  791. src_port = OMAP_DMA_PORT_TIPB;
  792. dest_port = OMAP_DMA_PORT_EMIFF;
  793. }
  794. if (cpu_class_is_omap2())
  795. sync_dev = mcbsp->dma_tx_sync;
  796. omap_set_dma_transfer_params(mcbsp->dma_tx_lch,
  797. OMAP_DMA_DATA_TYPE_S16,
  798. length >> 1, 1,
  799. OMAP_DMA_SYNC_ELEMENT,
  800. sync_dev, 0);
  801. omap_set_dma_dest_params(mcbsp->dma_tx_lch,
  802. src_port,
  803. OMAP_DMA_AMODE_CONSTANT,
  804. mcbsp->phys_base + OMAP_MCBSP_REG_DXR1,
  805. 0, 0);
  806. omap_set_dma_src_params(mcbsp->dma_tx_lch,
  807. dest_port,
  808. OMAP_DMA_AMODE_POST_INC,
  809. buffer,
  810. 0, 0);
  811. omap_start_dma(mcbsp->dma_tx_lch);
  812. wait_for_completion(&mcbsp->tx_dma_completion);
  813. return 0;
  814. }
  815. EXPORT_SYMBOL(omap_mcbsp_xmit_buffer);
  816. int omap_mcbsp_recv_buffer(unsigned int id, dma_addr_t buffer,
  817. unsigned int length)
  818. {
  819. struct omap_mcbsp *mcbsp;
  820. int dma_rx_ch;
  821. int src_port = 0;
  822. int dest_port = 0;
  823. int sync_dev = 0;
  824. if (!omap_mcbsp_check_valid_id(id)) {
  825. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  826. return -ENODEV;
  827. }
  828. mcbsp = id_to_mcbsp_ptr(id);
  829. if (omap_request_dma(mcbsp->dma_rx_sync, "McBSP RX",
  830. omap_mcbsp_rx_dma_callback,
  831. mcbsp,
  832. &dma_rx_ch)) {
  833. dev_err(mcbsp->dev, "Unable to request DMA channel for "
  834. "McBSP%d RX. Trying IRQ based RX\n",
  835. mcbsp->id);
  836. return -EAGAIN;
  837. }
  838. mcbsp->dma_rx_lch = dma_rx_ch;
  839. dev_err(mcbsp->dev, "McBSP%d RX DMA on channel %d\n", mcbsp->id,
  840. dma_rx_ch);
  841. init_completion(&mcbsp->rx_dma_completion);
  842. if (cpu_class_is_omap1()) {
  843. src_port = OMAP_DMA_PORT_TIPB;
  844. dest_port = OMAP_DMA_PORT_EMIFF;
  845. }
  846. if (cpu_class_is_omap2())
  847. sync_dev = mcbsp->dma_rx_sync;
  848. omap_set_dma_transfer_params(mcbsp->dma_rx_lch,
  849. OMAP_DMA_DATA_TYPE_S16,
  850. length >> 1, 1,
  851. OMAP_DMA_SYNC_ELEMENT,
  852. sync_dev, 0);
  853. omap_set_dma_src_params(mcbsp->dma_rx_lch,
  854. src_port,
  855. OMAP_DMA_AMODE_CONSTANT,
  856. mcbsp->phys_base + OMAP_MCBSP_REG_DRR1,
  857. 0, 0);
  858. omap_set_dma_dest_params(mcbsp->dma_rx_lch,
  859. dest_port,
  860. OMAP_DMA_AMODE_POST_INC,
  861. buffer,
  862. 0, 0);
  863. omap_start_dma(mcbsp->dma_rx_lch);
  864. wait_for_completion(&mcbsp->rx_dma_completion);
  865. return 0;
  866. }
  867. EXPORT_SYMBOL(omap_mcbsp_recv_buffer);
  868. /*
  869. * SPI wrapper.
  870. * Since SPI setup is much simpler than the generic McBSP one,
  871. * this wrapper just need an omap_mcbsp_spi_cfg structure as an input.
  872. * Once this is done, you can call omap_mcbsp_start().
  873. */
  874. void omap_mcbsp_set_spi_mode(unsigned int id,
  875. const struct omap_mcbsp_spi_cfg *spi_cfg)
  876. {
  877. struct omap_mcbsp *mcbsp;
  878. struct omap_mcbsp_reg_cfg mcbsp_cfg;
  879. if (!omap_mcbsp_check_valid_id(id)) {
  880. printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
  881. return;
  882. }
  883. mcbsp = id_to_mcbsp_ptr(id);
  884. memset(&mcbsp_cfg, 0, sizeof(struct omap_mcbsp_reg_cfg));
  885. /* SPI has only one frame */
  886. mcbsp_cfg.rcr1 |= (RWDLEN1(spi_cfg->word_length) | RFRLEN1(0));
  887. mcbsp_cfg.xcr1 |= (XWDLEN1(spi_cfg->word_length) | XFRLEN1(0));
  888. /* Clock stop mode */
  889. if (spi_cfg->clk_stp_mode == OMAP_MCBSP_CLK_STP_MODE_NO_DELAY)
  890. mcbsp_cfg.spcr1 |= (1 << 12);
  891. else
  892. mcbsp_cfg.spcr1 |= (3 << 11);
  893. /* Set clock parities */
  894. if (spi_cfg->rx_clock_polarity == OMAP_MCBSP_CLK_RISING)
  895. mcbsp_cfg.pcr0 |= CLKRP;
  896. else
  897. mcbsp_cfg.pcr0 &= ~CLKRP;
  898. if (spi_cfg->tx_clock_polarity == OMAP_MCBSP_CLK_RISING)
  899. mcbsp_cfg.pcr0 &= ~CLKXP;
  900. else
  901. mcbsp_cfg.pcr0 |= CLKXP;
  902. /* Set SCLKME to 0 and CLKSM to 1 */
  903. mcbsp_cfg.pcr0 &= ~SCLKME;
  904. mcbsp_cfg.srgr2 |= CLKSM;
  905. /* Set FSXP */
  906. if (spi_cfg->fsx_polarity == OMAP_MCBSP_FS_ACTIVE_HIGH)
  907. mcbsp_cfg.pcr0 &= ~FSXP;
  908. else
  909. mcbsp_cfg.pcr0 |= FSXP;
  910. if (spi_cfg->spi_mode == OMAP_MCBSP_SPI_MASTER) {
  911. mcbsp_cfg.pcr0 |= CLKXM;
  912. mcbsp_cfg.srgr1 |= CLKGDV(spi_cfg->clk_div - 1);
  913. mcbsp_cfg.pcr0 |= FSXM;
  914. mcbsp_cfg.srgr2 &= ~FSGM;
  915. mcbsp_cfg.xcr2 |= XDATDLY(1);
  916. mcbsp_cfg.rcr2 |= RDATDLY(1);
  917. } else {
  918. mcbsp_cfg.pcr0 &= ~CLKXM;
  919. mcbsp_cfg.srgr1 |= CLKGDV(1);
  920. mcbsp_cfg.pcr0 &= ~FSXM;
  921. mcbsp_cfg.xcr2 &= ~XDATDLY(3);
  922. mcbsp_cfg.rcr2 &= ~RDATDLY(3);
  923. }
  924. mcbsp_cfg.xcr2 &= ~XPHASE;
  925. mcbsp_cfg.rcr2 &= ~RPHASE;
  926. omap_mcbsp_config(id, &mcbsp_cfg);
  927. }
  928. EXPORT_SYMBOL(omap_mcbsp_set_spi_mode);
  929. #ifdef CONFIG_ARCH_OMAP3
  930. #define max_thres(m) (mcbsp->pdata->buffer_size)
  931. #define valid_threshold(m, val) ((val) <= max_thres(m))
  932. #define THRESHOLD_PROP_BUILDER(prop) \
  933. static ssize_t prop##_show(struct device *dev, \
  934. struct device_attribute *attr, char *buf) \
  935. { \
  936. struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
  937. \
  938. return sprintf(buf, "%u\n", mcbsp->prop); \
  939. } \
  940. \
  941. static ssize_t prop##_store(struct device *dev, \
  942. struct device_attribute *attr, \
  943. const char *buf, size_t size) \
  944. { \
  945. struct omap_mcbsp *mcbsp = dev_get_drvdata(dev); \
  946. unsigned long val; \
  947. int status; \
  948. \
  949. status = strict_strtoul(buf, 0, &val); \
  950. if (status) \
  951. return status; \
  952. \
  953. if (!valid_threshold(mcbsp, val)) \
  954. return -EDOM; \
  955. \
  956. mcbsp->prop = val; \
  957. return size; \
  958. } \
  959. \
  960. static DEVICE_ATTR(prop, 0644, prop##_show, prop##_store);
  961. THRESHOLD_PROP_BUILDER(max_tx_thres);
  962. THRESHOLD_PROP_BUILDER(max_rx_thres);
  963. static const char *dma_op_modes[] = {
  964. "element", "threshold", "frame",
  965. };
  966. static ssize_t dma_op_mode_show(struct device *dev,
  967. struct device_attribute *attr, char *buf)
  968. {
  969. struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
  970. int dma_op_mode, i = 0;
  971. ssize_t len = 0;
  972. const char * const *s;
  973. dma_op_mode = mcbsp->dma_op_mode;
  974. for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++) {
  975. if (dma_op_mode == i)
  976. len += sprintf(buf + len, "[%s] ", *s);
  977. else
  978. len += sprintf(buf + len, "%s ", *s);
  979. }
  980. len += sprintf(buf + len, "\n");
  981. return len;
  982. }
  983. static ssize_t dma_op_mode_store(struct device *dev,
  984. struct device_attribute *attr,
  985. const char *buf, size_t size)
  986. {
  987. struct omap_mcbsp *mcbsp = dev_get_drvdata(dev);
  988. const char * const *s;
  989. int i = 0;
  990. for (s = &dma_op_modes[i]; i < ARRAY_SIZE(dma_op_modes); s++, i++)
  991. if (sysfs_streq(buf, *s))
  992. break;
  993. if (i == ARRAY_SIZE(dma_op_modes))
  994. return -EINVAL;
  995. spin_lock_irq(&mcbsp->lock);
  996. if (!mcbsp->free) {
  997. size = -EBUSY;
  998. goto unlock;
  999. }
  1000. mcbsp->dma_op_mode = i;
  1001. unlock:
  1002. spin_unlock_irq(&mcbsp->lock);
  1003. return size;
  1004. }
  1005. static DEVICE_ATTR(dma_op_mode, 0644, dma_op_mode_show, dma_op_mode_store);
  1006. static const struct attribute *additional_attrs[] = {
  1007. &dev_attr_max_tx_thres.attr,
  1008. &dev_attr_max_rx_thres.attr,
  1009. &dev_attr_dma_op_mode.attr,
  1010. NULL,
  1011. };
  1012. static const struct attribute_group additional_attr_group = {
  1013. .attrs = (struct attribute **)additional_attrs,
  1014. };
  1015. static inline int __devinit omap_additional_add(struct device *dev)
  1016. {
  1017. return sysfs_create_group(&dev->kobj, &additional_attr_group);
  1018. }
  1019. static inline void __devexit omap_additional_remove(struct device *dev)
  1020. {
  1021. sysfs_remove_group(&dev->kobj, &additional_attr_group);
  1022. }
  1023. static inline void __devinit omap34xx_device_init(struct omap_mcbsp *mcbsp)
  1024. {
  1025. mcbsp->dma_op_mode = MCBSP_DMA_MODE_ELEMENT;
  1026. if (cpu_is_omap34xx()) {
  1027. mcbsp->max_tx_thres = max_thres(mcbsp);
  1028. mcbsp->max_rx_thres = max_thres(mcbsp);
  1029. /*
  1030. * REVISIT: Set dmap_op_mode to THRESHOLD as default
  1031. * for mcbsp2 instances.
  1032. */
  1033. if (omap_additional_add(mcbsp->dev))
  1034. dev_warn(mcbsp->dev,
  1035. "Unable to create additional controls\n");
  1036. } else {
  1037. mcbsp->max_tx_thres = -EINVAL;
  1038. mcbsp->max_rx_thres = -EINVAL;
  1039. }
  1040. }
  1041. static inline void __devexit omap34xx_device_exit(struct omap_mcbsp *mcbsp)
  1042. {
  1043. if (cpu_is_omap34xx())
  1044. omap_additional_remove(mcbsp->dev);
  1045. }
  1046. #else
  1047. static inline void __devinit omap34xx_device_init(struct omap_mcbsp *mcbsp) {}
  1048. static inline void __devexit omap34xx_device_exit(struct omap_mcbsp *mcbsp) {}
  1049. #endif /* CONFIG_ARCH_OMAP3 */
  1050. /*
  1051. * McBSP1 and McBSP3 are directly mapped on 1610 and 1510.
  1052. * 730 has only 2 McBSP, and both of them are MPU peripherals.
  1053. */
  1054. static int __devinit omap_mcbsp_probe(struct platform_device *pdev)
  1055. {
  1056. struct omap_mcbsp_platform_data *pdata = pdev->dev.platform_data;
  1057. struct omap_mcbsp *mcbsp;
  1058. int id = pdev->id - 1;
  1059. int ret = 0;
  1060. if (!pdata) {
  1061. dev_err(&pdev->dev, "McBSP device initialized without"
  1062. "platform data\n");
  1063. ret = -EINVAL;
  1064. goto exit;
  1065. }
  1066. dev_dbg(&pdev->dev, "Initializing OMAP McBSP (%d).\n", pdev->id);
  1067. if (id >= omap_mcbsp_count) {
  1068. dev_err(&pdev->dev, "Invalid McBSP device id (%d)\n", id);
  1069. ret = -EINVAL;
  1070. goto exit;
  1071. }
  1072. mcbsp = kzalloc(sizeof(struct omap_mcbsp), GFP_KERNEL);
  1073. if (!mcbsp) {
  1074. ret = -ENOMEM;
  1075. goto exit;
  1076. }
  1077. spin_lock_init(&mcbsp->lock);
  1078. mcbsp->id = id + 1;
  1079. mcbsp->free = 1;
  1080. mcbsp->dma_tx_lch = -1;
  1081. mcbsp->dma_rx_lch = -1;
  1082. mcbsp->phys_base = pdata->phys_base;
  1083. mcbsp->io_base = ioremap(pdata->phys_base, SZ_4K);
  1084. if (!mcbsp->io_base) {
  1085. ret = -ENOMEM;
  1086. goto err_ioremap;
  1087. }
  1088. /* Default I/O is IRQ based */
  1089. mcbsp->io_type = OMAP_MCBSP_IRQ_IO;
  1090. mcbsp->tx_irq = pdata->tx_irq;
  1091. mcbsp->rx_irq = pdata->rx_irq;
  1092. mcbsp->dma_rx_sync = pdata->dma_rx_sync;
  1093. mcbsp->dma_tx_sync = pdata->dma_tx_sync;
  1094. mcbsp->iclk = clk_get(&pdev->dev, "ick");
  1095. if (IS_ERR(mcbsp->iclk)) {
  1096. ret = PTR_ERR(mcbsp->iclk);
  1097. dev_err(&pdev->dev, "unable to get ick: %d\n", ret);
  1098. goto err_iclk;
  1099. }
  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_fclk;
  1105. }
  1106. mcbsp->pdata = pdata;
  1107. mcbsp->dev = &pdev->dev;
  1108. mcbsp_ptr[id] = mcbsp;
  1109. platform_set_drvdata(pdev, mcbsp);
  1110. /* Initialize mcbsp properties for OMAP34XX if needed / applicable */
  1111. omap34xx_device_init(mcbsp);
  1112. return 0;
  1113. err_fclk:
  1114. clk_put(mcbsp->iclk);
  1115. err_iclk:
  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_disable(mcbsp->fclk);
  1132. clk_disable(mcbsp->iclk);
  1133. clk_put(mcbsp->fclk);
  1134. clk_put(mcbsp->iclk);
  1135. iounmap(mcbsp->io_base);
  1136. mcbsp->fclk = NULL;
  1137. mcbsp->iclk = NULL;
  1138. mcbsp->free = 0;
  1139. mcbsp->dev = NULL;
  1140. }
  1141. return 0;
  1142. }
  1143. static struct platform_driver omap_mcbsp_driver = {
  1144. .probe = omap_mcbsp_probe,
  1145. .remove = __devexit_p(omap_mcbsp_remove),
  1146. .driver = {
  1147. .name = "omap-mcbsp",
  1148. },
  1149. };
  1150. int __init omap_mcbsp_init(void)
  1151. {
  1152. /* Register the McBSP driver */
  1153. return platform_driver_register(&omap_mcbsp_driver);
  1154. }