wm0010.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. /*
  2. * wm0010.c -- WM0010 DSP Driver
  3. *
  4. * Copyright 2012 Wolfson Microelectronics PLC.
  5. *
  6. * Authors: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. * Dimitris Papastamos <dp@opensource.wolfsonmicro.com>
  8. * Scott Ling <sl@opensource.wolfsonmicro.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/irqreturn.h>
  18. #include <linux/init.h>
  19. #include <linux/spi/spi.h>
  20. #include <linux/firmware.h>
  21. #include <linux/delay.h>
  22. #include <linux/fs.h>
  23. #include <linux/miscdevice.h>
  24. #include <linux/gpio.h>
  25. #include <linux/regulator/consumer.h>
  26. #include <linux/mutex.h>
  27. #include <linux/workqueue.h>
  28. #include <sound/soc.h>
  29. #include <sound/wm0010.h>
  30. #define DEVICE_ID_WM0010 10
  31. /* We only support v1 of the .dfw INFO record */
  32. #define INFO_VERSION 1
  33. enum dfw_cmd {
  34. DFW_CMD_FUSE = 0x01,
  35. DFW_CMD_CODE_HDR,
  36. DFW_CMD_CODE_DATA,
  37. DFW_CMD_PLL,
  38. DFW_CMD_INFO = 0xff
  39. };
  40. struct dfw_binrec {
  41. u8 command;
  42. u32 length:24;
  43. u32 address;
  44. uint8_t data[0];
  45. } __packed;
  46. struct dfw_inforec {
  47. u8 info_version;
  48. u8 tool_major_version;
  49. u8 tool_minor_version;
  50. u8 dsp_target;
  51. };
  52. struct dfw_pllrec {
  53. u8 command;
  54. u32 length:24;
  55. u32 address;
  56. u32 clkctrl1;
  57. u32 clkctrl2;
  58. u32 clkctrl3;
  59. u32 ldetctrl;
  60. u32 uart_div;
  61. u32 spi_div;
  62. } __packed;
  63. static struct pll_clock_map {
  64. int max_sysclk;
  65. int max_pll_spi_speed;
  66. u32 pll_clkctrl1;
  67. } pll_clock_map[] = { /* Dividers */
  68. { 22000000, 26000000, 0x00201f11 }, /* 2,32,2 */
  69. { 18000000, 26000000, 0x00203f21 }, /* 2,64,4 */
  70. { 14000000, 26000000, 0x00202620 }, /* 1,39,4 */
  71. { 10000000, 22000000, 0x00203120 }, /* 1,50,4 */
  72. { 6500000, 22000000, 0x00204520 }, /* 1,70,4 */
  73. { 5500000, 22000000, 0x00103f10 }, /* 1,64,2 */
  74. };
  75. enum wm0010_state {
  76. WM0010_POWER_OFF,
  77. WM0010_OUT_OF_RESET,
  78. WM0010_BOOTROM,
  79. WM0010_STAGE2,
  80. WM0010_FIRMWARE,
  81. };
  82. struct wm0010_priv {
  83. struct snd_soc_codec *codec;
  84. struct mutex lock;
  85. struct device *dev;
  86. struct wm0010_pdata pdata;
  87. int gpio_reset;
  88. int gpio_reset_value;
  89. struct regulator_bulk_data core_supplies[2];
  90. struct regulator *dbvdd;
  91. int sysclk;
  92. enum wm0010_state state;
  93. bool boot_failed;
  94. bool ready;
  95. bool pll_running;
  96. int max_spi_freq;
  97. int board_max_spi_speed;
  98. u32 pll_clkctrl1;
  99. spinlock_t irq_lock;
  100. int irq;
  101. struct completion boot_completion;
  102. };
  103. struct wm0010_spi_msg {
  104. struct spi_message m;
  105. struct spi_transfer t;
  106. u8 *tx_buf;
  107. u8 *rx_buf;
  108. size_t len;
  109. };
  110. static const struct snd_soc_dapm_widget wm0010_dapm_widgets[] = {
  111. SND_SOC_DAPM_SUPPLY("CLKIN", SND_SOC_NOPM, 0, 0, NULL, 0),
  112. };
  113. static const struct snd_soc_dapm_route wm0010_dapm_routes[] = {
  114. { "SDI2 Capture", NULL, "SDI1 Playback" },
  115. { "SDI1 Capture", NULL, "SDI2 Playback" },
  116. { "SDI1 Capture", NULL, "CLKIN" },
  117. { "SDI2 Capture", NULL, "CLKIN" },
  118. { "SDI1 Playback", NULL, "CLKIN" },
  119. { "SDI2 Playback", NULL, "CLKIN" },
  120. };
  121. static const char *wm0010_state_to_str(enum wm0010_state state)
  122. {
  123. const char *state_to_str[] = {
  124. "Power off",
  125. "Out of reset",
  126. "Boot ROM",
  127. "Stage2",
  128. "Firmware"
  129. };
  130. if (state < 0 || state >= ARRAY_SIZE(state_to_str))
  131. return "null";
  132. return state_to_str[state];
  133. }
  134. /* Called with wm0010->lock held */
  135. static void wm0010_halt(struct snd_soc_codec *codec)
  136. {
  137. struct wm0010_priv *wm0010 = snd_soc_codec_get_drvdata(codec);
  138. unsigned long flags;
  139. enum wm0010_state state;
  140. /* Fetch the wm0010 state */
  141. spin_lock_irqsave(&wm0010->irq_lock, flags);
  142. state = wm0010->state;
  143. spin_unlock_irqrestore(&wm0010->irq_lock, flags);
  144. switch (state) {
  145. case WM0010_POWER_OFF:
  146. /* If there's nothing to do, bail out */
  147. return;
  148. case WM0010_OUT_OF_RESET:
  149. case WM0010_BOOTROM:
  150. case WM0010_STAGE2:
  151. case WM0010_FIRMWARE:
  152. /* Remember to put chip back into reset */
  153. gpio_set_value_cansleep(wm0010->gpio_reset,
  154. wm0010->gpio_reset_value);
  155. /* Disable the regulators */
  156. regulator_disable(wm0010->dbvdd);
  157. regulator_bulk_disable(ARRAY_SIZE(wm0010->core_supplies),
  158. wm0010->core_supplies);
  159. break;
  160. }
  161. spin_lock_irqsave(&wm0010->irq_lock, flags);
  162. wm0010->state = WM0010_POWER_OFF;
  163. spin_unlock_irqrestore(&wm0010->irq_lock, flags);
  164. }
  165. struct wm0010_boot_xfer {
  166. struct list_head list;
  167. struct snd_soc_codec *codec;
  168. struct completion *done;
  169. struct spi_message m;
  170. struct spi_transfer t;
  171. };
  172. /* Called with wm0010->lock held */
  173. static void wm0010_mark_boot_failure(struct wm0010_priv *wm0010)
  174. {
  175. enum wm0010_state state;
  176. unsigned long flags;
  177. spin_lock_irqsave(&wm0010->irq_lock, flags);
  178. state = wm0010->state;
  179. spin_unlock_irqrestore(&wm0010->irq_lock, flags);
  180. dev_err(wm0010->dev, "Failed to transition from `%s' state to `%s' state\n",
  181. wm0010_state_to_str(state), wm0010_state_to_str(state + 1));
  182. wm0010->boot_failed = true;
  183. }
  184. static void wm0010_boot_xfer_complete(void *data)
  185. {
  186. struct wm0010_boot_xfer *xfer = data;
  187. struct snd_soc_codec *codec = xfer->codec;
  188. struct wm0010_priv *wm0010 = snd_soc_codec_get_drvdata(codec);
  189. u32 *out32 = xfer->t.rx_buf;
  190. int i;
  191. if (xfer->m.status != 0) {
  192. dev_err(codec->dev, "SPI transfer failed: %d\n",
  193. xfer->m.status);
  194. wm0010_mark_boot_failure(wm0010);
  195. if (xfer->done)
  196. complete(xfer->done);
  197. return;
  198. }
  199. for (i = 0; i < xfer->t.len / 4; i++) {
  200. dev_dbg(codec->dev, "%d: %04x\n", i, out32[i]);
  201. switch (be32_to_cpu(out32[i])) {
  202. case 0xe0e0e0e0:
  203. dev_err(codec->dev,
  204. "%d: ROM error reported in stage 2\n", i);
  205. wm0010_mark_boot_failure(wm0010);
  206. break;
  207. case 0x55555555:
  208. if (wm0010->state < WM0010_STAGE2)
  209. break;
  210. dev_err(codec->dev,
  211. "%d: ROM bootloader running in stage 2\n", i);
  212. wm0010_mark_boot_failure(wm0010);
  213. break;
  214. case 0x0fed0000:
  215. dev_dbg(codec->dev, "Stage2 loader running\n");
  216. break;
  217. case 0x0fed0007:
  218. dev_dbg(codec->dev, "CODE_HDR packet received\n");
  219. break;
  220. case 0x0fed0008:
  221. dev_dbg(codec->dev, "CODE_DATA packet received\n");
  222. break;
  223. case 0x0fed0009:
  224. dev_dbg(codec->dev, "Download complete\n");
  225. break;
  226. case 0x0fed000c:
  227. dev_dbg(codec->dev, "Application start\n");
  228. break;
  229. case 0x0fed000e:
  230. dev_dbg(codec->dev, "PLL packet received\n");
  231. wm0010->pll_running = true;
  232. break;
  233. case 0x0fed0025:
  234. dev_err(codec->dev, "Device reports image too long\n");
  235. wm0010_mark_boot_failure(wm0010);
  236. break;
  237. case 0x0fed002c:
  238. dev_err(codec->dev, "Device reports bad SPI packet\n");
  239. wm0010_mark_boot_failure(wm0010);
  240. break;
  241. case 0x0fed0031:
  242. dev_err(codec->dev, "Device reports SPI read overflow\n");
  243. wm0010_mark_boot_failure(wm0010);
  244. break;
  245. case 0x0fed0032:
  246. dev_err(codec->dev, "Device reports SPI underclock\n");
  247. wm0010_mark_boot_failure(wm0010);
  248. break;
  249. case 0x0fed0033:
  250. dev_err(codec->dev, "Device reports bad header packet\n");
  251. wm0010_mark_boot_failure(wm0010);
  252. break;
  253. case 0x0fed0034:
  254. dev_err(codec->dev, "Device reports invalid packet type\n");
  255. wm0010_mark_boot_failure(wm0010);
  256. break;
  257. case 0x0fed0035:
  258. dev_err(codec->dev, "Device reports data before header error\n");
  259. wm0010_mark_boot_failure(wm0010);
  260. break;
  261. case 0x0fed0038:
  262. dev_err(codec->dev, "Device reports invalid PLL packet\n");
  263. break;
  264. case 0x0fed003a:
  265. dev_err(codec->dev, "Device reports packet alignment error\n");
  266. wm0010_mark_boot_failure(wm0010);
  267. break;
  268. default:
  269. dev_err(codec->dev, "Unrecognised return 0x%x\n",
  270. be32_to_cpu(out32[i]));
  271. wm0010_mark_boot_failure(wm0010);
  272. break;
  273. }
  274. if (wm0010->boot_failed)
  275. break;
  276. }
  277. if (xfer->done)
  278. complete(xfer->done);
  279. }
  280. static void byte_swap_64(u64 *data_in, u64 *data_out, u32 len)
  281. {
  282. int i;
  283. for (i = 0; i < len / 8; i++)
  284. data_out[i] = cpu_to_be64(le64_to_cpu(data_in[i]));
  285. }
  286. static int wm0010_firmware_load(const char *name, struct snd_soc_codec *codec)
  287. {
  288. struct spi_device *spi = to_spi_device(codec->dev);
  289. struct wm0010_priv *wm0010 = snd_soc_codec_get_drvdata(codec);
  290. struct list_head xfer_list;
  291. struct wm0010_boot_xfer *xfer;
  292. int ret;
  293. struct completion done;
  294. const struct firmware *fw;
  295. const struct dfw_binrec *rec;
  296. const struct dfw_inforec *inforec;
  297. u64 *img;
  298. u8 *out, dsp;
  299. u32 len, offset;
  300. INIT_LIST_HEAD(&xfer_list);
  301. ret = request_firmware(&fw, name, codec->dev);
  302. if (ret != 0) {
  303. dev_err(codec->dev, "Failed to request application(%s): %d\n",
  304. name, ret);
  305. return ret;
  306. }
  307. rec = (const struct dfw_binrec *)fw->data;
  308. inforec = (const struct dfw_inforec *)rec->data;
  309. offset = 0;
  310. dsp = inforec->dsp_target;
  311. wm0010->boot_failed = false;
  312. BUG_ON(!list_empty(&xfer_list));
  313. init_completion(&done);
  314. /* First record should be INFO */
  315. if (rec->command != DFW_CMD_INFO) {
  316. dev_err(codec->dev, "First record not INFO\r\n");
  317. ret = -EINVAL;
  318. goto abort;
  319. }
  320. if (inforec->info_version != INFO_VERSION) {
  321. dev_err(codec->dev,
  322. "Unsupported version (%02d) of INFO record\r\n",
  323. inforec->info_version);
  324. ret = -EINVAL;
  325. goto abort;
  326. }
  327. dev_dbg(codec->dev, "Version v%02d INFO record found\r\n",
  328. inforec->info_version);
  329. /* Check it's a DSP file */
  330. if (dsp != DEVICE_ID_WM0010) {
  331. dev_err(codec->dev, "Not a WM0010 firmware file.\r\n");
  332. ret = -EINVAL;
  333. goto abort;
  334. }
  335. /* Skip the info record as we don't need to send it */
  336. offset += ((rec->length) + 8);
  337. rec = (void *)&rec->data[rec->length];
  338. while (offset < fw->size) {
  339. dev_dbg(codec->dev,
  340. "Packet: command %d, data length = 0x%x\r\n",
  341. rec->command, rec->length);
  342. len = rec->length + 8;
  343. xfer = kzalloc(sizeof(*xfer), GFP_KERNEL);
  344. if (!xfer) {
  345. dev_err(codec->dev, "Failed to allocate xfer\n");
  346. ret = -ENOMEM;
  347. goto abort;
  348. }
  349. xfer->codec = codec;
  350. list_add_tail(&xfer->list, &xfer_list);
  351. out = kzalloc(len, GFP_KERNEL | GFP_DMA);
  352. if (!out) {
  353. dev_err(codec->dev,
  354. "Failed to allocate RX buffer\n");
  355. ret = -ENOMEM;
  356. goto abort1;
  357. }
  358. xfer->t.rx_buf = out;
  359. img = kzalloc(len, GFP_KERNEL | GFP_DMA);
  360. if (!img) {
  361. dev_err(codec->dev,
  362. "Failed to allocate image buffer\n");
  363. ret = -ENOMEM;
  364. goto abort1;
  365. }
  366. xfer->t.tx_buf = img;
  367. byte_swap_64((u64 *)&rec->command, img, len);
  368. spi_message_init(&xfer->m);
  369. xfer->m.complete = wm0010_boot_xfer_complete;
  370. xfer->m.context = xfer;
  371. xfer->t.len = len;
  372. xfer->t.bits_per_word = 8;
  373. if (!wm0010->pll_running) {
  374. xfer->t.speed_hz = wm0010->sysclk / 6;
  375. } else {
  376. xfer->t.speed_hz = wm0010->max_spi_freq;
  377. if (wm0010->board_max_spi_speed &&
  378. (wm0010->board_max_spi_speed < wm0010->max_spi_freq))
  379. xfer->t.speed_hz = wm0010->board_max_spi_speed;
  380. }
  381. /* Store max usable spi frequency for later use */
  382. wm0010->max_spi_freq = xfer->t.speed_hz;
  383. spi_message_add_tail(&xfer->t, &xfer->m);
  384. offset += ((rec->length) + 8);
  385. rec = (void *)&rec->data[rec->length];
  386. if (offset >= fw->size) {
  387. dev_dbg(codec->dev, "All transfers scheduled\n");
  388. xfer->done = &done;
  389. }
  390. ret = spi_async(spi, &xfer->m);
  391. if (ret != 0) {
  392. dev_err(codec->dev, "Write failed: %d\n", ret);
  393. goto abort1;
  394. }
  395. if (wm0010->boot_failed) {
  396. dev_dbg(codec->dev, "Boot fail!\n");
  397. ret = -EINVAL;
  398. goto abort1;
  399. }
  400. }
  401. wait_for_completion(&done);
  402. ret = 0;
  403. abort1:
  404. while (!list_empty(&xfer_list)) {
  405. xfer = list_first_entry(&xfer_list, struct wm0010_boot_xfer,
  406. list);
  407. kfree(xfer->t.rx_buf);
  408. kfree(xfer->t.tx_buf);
  409. list_del(&xfer->list);
  410. kfree(xfer);
  411. }
  412. abort:
  413. release_firmware(fw);
  414. return ret;
  415. }
  416. static int wm0010_stage2_load(struct snd_soc_codec *codec)
  417. {
  418. struct spi_device *spi = to_spi_device(codec->dev);
  419. struct wm0010_priv *wm0010 = snd_soc_codec_get_drvdata(codec);
  420. const struct firmware *fw;
  421. struct spi_message m;
  422. struct spi_transfer t;
  423. u32 *img;
  424. u8 *out;
  425. int i;
  426. int ret = 0;
  427. ret = request_firmware(&fw, "wm0010_stage2.bin", codec->dev);
  428. if (ret != 0) {
  429. dev_err(codec->dev, "Failed to request stage2 loader: %d\n",
  430. ret);
  431. return ret;
  432. }
  433. dev_dbg(codec->dev, "Downloading %zu byte stage 2 loader\n", fw->size);
  434. /* Copy to local buffer first as vmalloc causes problems for dma */
  435. img = kzalloc(fw->size, GFP_KERNEL | GFP_DMA);
  436. if (!img) {
  437. dev_err(codec->dev, "Failed to allocate image buffer\n");
  438. ret = -ENOMEM;
  439. goto abort2;
  440. }
  441. out = kzalloc(fw->size, GFP_KERNEL | GFP_DMA);
  442. if (!out) {
  443. dev_err(codec->dev, "Failed to allocate output buffer\n");
  444. ret = -ENOMEM;
  445. goto abort1;
  446. }
  447. memcpy(img, &fw->data[0], fw->size);
  448. spi_message_init(&m);
  449. memset(&t, 0, sizeof(t));
  450. t.rx_buf = out;
  451. t.tx_buf = img;
  452. t.len = fw->size;
  453. t.bits_per_word = 8;
  454. t.speed_hz = wm0010->sysclk / 10;
  455. spi_message_add_tail(&t, &m);
  456. dev_dbg(codec->dev, "Starting initial download at %dHz\n",
  457. t.speed_hz);
  458. ret = spi_sync(spi, &m);
  459. if (ret != 0) {
  460. dev_err(codec->dev, "Initial download failed: %d\n", ret);
  461. goto abort;
  462. }
  463. /* Look for errors from the boot ROM */
  464. for (i = 0; i < fw->size; i++) {
  465. if (out[i] != 0x55) {
  466. dev_err(codec->dev, "Boot ROM error: %x in %d\n",
  467. out[i], i);
  468. wm0010_mark_boot_failure(wm0010);
  469. ret = -EBUSY;
  470. goto abort;
  471. }
  472. }
  473. abort:
  474. kfree(out);
  475. abort1:
  476. kfree(img);
  477. abort2:
  478. release_firmware(fw);
  479. return ret;
  480. }
  481. static int wm0010_boot(struct snd_soc_codec *codec)
  482. {
  483. struct spi_device *spi = to_spi_device(codec->dev);
  484. struct wm0010_priv *wm0010 = snd_soc_codec_get_drvdata(codec);
  485. unsigned long flags;
  486. int ret;
  487. const struct firmware *fw;
  488. struct spi_message m;
  489. struct spi_transfer t;
  490. struct dfw_pllrec pll_rec;
  491. u32 *p, len;
  492. u64 *img_swap;
  493. u8 *out;
  494. int i;
  495. spin_lock_irqsave(&wm0010->irq_lock, flags);
  496. if (wm0010->state != WM0010_POWER_OFF)
  497. dev_warn(wm0010->dev, "DSP already powered up!\n");
  498. spin_unlock_irqrestore(&wm0010->irq_lock, flags);
  499. if (wm0010->sysclk > 26000000) {
  500. dev_err(codec->dev, "Max DSP clock frequency is 26MHz\n");
  501. ret = -ECANCELED;
  502. goto err;
  503. }
  504. mutex_lock(&wm0010->lock);
  505. wm0010->pll_running = false;
  506. dev_dbg(codec->dev, "max_spi_freq: %d\n", wm0010->max_spi_freq);
  507. ret = regulator_bulk_enable(ARRAY_SIZE(wm0010->core_supplies),
  508. wm0010->core_supplies);
  509. if (ret != 0) {
  510. dev_err(&spi->dev, "Failed to enable core supplies: %d\n",
  511. ret);
  512. mutex_unlock(&wm0010->lock);
  513. goto err;
  514. }
  515. ret = regulator_enable(wm0010->dbvdd);
  516. if (ret != 0) {
  517. dev_err(&spi->dev, "Failed to enable DBVDD: %d\n", ret);
  518. goto err_core;
  519. }
  520. /* Release reset */
  521. gpio_set_value_cansleep(wm0010->gpio_reset, !wm0010->gpio_reset_value);
  522. spin_lock_irqsave(&wm0010->irq_lock, flags);
  523. wm0010->state = WM0010_OUT_OF_RESET;
  524. spin_unlock_irqrestore(&wm0010->irq_lock, flags);
  525. /* First the bootloader */
  526. ret = request_firmware(&fw, "wm0010_stage2.bin", codec->dev);
  527. if (ret != 0) {
  528. dev_err(codec->dev, "Failed to request stage2 loader: %d\n",
  529. ret);
  530. goto abort;
  531. }
  532. if (!wait_for_completion_timeout(&wm0010->boot_completion,
  533. msecs_to_jiffies(20)))
  534. dev_err(codec->dev, "Failed to get interrupt from DSP\n");
  535. spin_lock_irqsave(&wm0010->irq_lock, flags);
  536. wm0010->state = WM0010_BOOTROM;
  537. spin_unlock_irqrestore(&wm0010->irq_lock, flags);
  538. ret = wm0010_stage2_load(codec);
  539. if (ret)
  540. goto abort;
  541. if (!wait_for_completion_timeout(&wm0010->boot_completion,
  542. msecs_to_jiffies(20)))
  543. dev_err(codec->dev, "Failed to get interrupt from DSP loader.\n");
  544. spin_lock_irqsave(&wm0010->irq_lock, flags);
  545. wm0010->state = WM0010_STAGE2;
  546. spin_unlock_irqrestore(&wm0010->irq_lock, flags);
  547. /* Only initialise PLL if max_spi_freq initialised */
  548. if (wm0010->max_spi_freq) {
  549. /* Initialise a PLL record */
  550. memset(&pll_rec, 0, sizeof(pll_rec));
  551. pll_rec.command = DFW_CMD_PLL;
  552. pll_rec.length = (sizeof(pll_rec) - 8);
  553. /* On wm0010 only the CLKCTRL1 value is used */
  554. pll_rec.clkctrl1 = wm0010->pll_clkctrl1;
  555. ret = -ENOMEM;
  556. len = pll_rec.length + 8;
  557. out = kzalloc(len, GFP_KERNEL | GFP_DMA);
  558. if (!out) {
  559. dev_err(codec->dev,
  560. "Failed to allocate RX buffer\n");
  561. goto abort;
  562. }
  563. img_swap = kzalloc(len, GFP_KERNEL | GFP_DMA);
  564. if (!img_swap) {
  565. dev_err(codec->dev,
  566. "Failed to allocate image buffer\n");
  567. goto abort;
  568. }
  569. /* We need to re-order for 0010 */
  570. byte_swap_64((u64 *)&pll_rec, img_swap, len);
  571. spi_message_init(&m);
  572. memset(&t, 0, sizeof(t));
  573. t.rx_buf = out;
  574. t.tx_buf = img_swap;
  575. t.len = len;
  576. t.bits_per_word = 8;
  577. t.speed_hz = wm0010->sysclk / 6;
  578. spi_message_add_tail(&t, &m);
  579. ret = spi_sync(spi, &m);
  580. if (ret != 0) {
  581. dev_err(codec->dev, "First PLL write failed: %d\n", ret);
  582. goto abort;
  583. }
  584. /* Use a second send of the message to get the return status */
  585. ret = spi_sync(spi, &m);
  586. if (ret != 0) {
  587. dev_err(codec->dev, "Second PLL write failed: %d\n", ret);
  588. goto abort;
  589. }
  590. p = (u32 *)out;
  591. /* Look for PLL active code from the DSP */
  592. for (i = 0; i < len / 4; i++) {
  593. if (*p == 0x0e00ed0f) {
  594. dev_dbg(codec->dev, "PLL packet received\n");
  595. wm0010->pll_running = true;
  596. break;
  597. }
  598. p++;
  599. }
  600. kfree(img_swap);
  601. kfree(out);
  602. } else
  603. dev_dbg(codec->dev, "Not enabling DSP PLL.");
  604. ret = wm0010_firmware_load("wm0010.dfw", codec);
  605. if (ret != 0)
  606. goto abort;
  607. spin_lock_irqsave(&wm0010->irq_lock, flags);
  608. wm0010->state = WM0010_FIRMWARE;
  609. spin_unlock_irqrestore(&wm0010->irq_lock, flags);
  610. mutex_unlock(&wm0010->lock);
  611. return 0;
  612. abort:
  613. /* Put the chip back into reset */
  614. wm0010_halt(codec);
  615. mutex_unlock(&wm0010->lock);
  616. return ret;
  617. err_core:
  618. mutex_unlock(&wm0010->lock);
  619. regulator_bulk_disable(ARRAY_SIZE(wm0010->core_supplies),
  620. wm0010->core_supplies);
  621. err:
  622. return ret;
  623. }
  624. static int wm0010_set_bias_level(struct snd_soc_codec *codec,
  625. enum snd_soc_bias_level level)
  626. {
  627. struct wm0010_priv *wm0010 = snd_soc_codec_get_drvdata(codec);
  628. switch (level) {
  629. case SND_SOC_BIAS_ON:
  630. if (codec->dapm.bias_level == SND_SOC_BIAS_PREPARE)
  631. wm0010_boot(codec);
  632. break;
  633. case SND_SOC_BIAS_PREPARE:
  634. break;
  635. case SND_SOC_BIAS_STANDBY:
  636. if (codec->dapm.bias_level == SND_SOC_BIAS_PREPARE) {
  637. mutex_lock(&wm0010->lock);
  638. wm0010_halt(codec);
  639. mutex_unlock(&wm0010->lock);
  640. }
  641. break;
  642. case SND_SOC_BIAS_OFF:
  643. break;
  644. }
  645. codec->dapm.bias_level = level;
  646. return 0;
  647. }
  648. static int wm0010_set_sysclk(struct snd_soc_codec *codec, int source,
  649. int clk_id, unsigned int freq, int dir)
  650. {
  651. struct wm0010_priv *wm0010 = snd_soc_codec_get_drvdata(codec);
  652. unsigned int i;
  653. wm0010->sysclk = freq;
  654. if (freq < pll_clock_map[ARRAY_SIZE(pll_clock_map)-1].max_sysclk) {
  655. wm0010->max_spi_freq = 0;
  656. } else {
  657. for (i = 0; i < ARRAY_SIZE(pll_clock_map); i++)
  658. if (freq >= pll_clock_map[i].max_sysclk)
  659. break;
  660. wm0010->max_spi_freq = pll_clock_map[i].max_pll_spi_speed;
  661. wm0010->pll_clkctrl1 = pll_clock_map[i].pll_clkctrl1;
  662. }
  663. return 0;
  664. }
  665. static int wm0010_probe(struct snd_soc_codec *codec);
  666. static struct snd_soc_codec_driver soc_codec_dev_wm0010 = {
  667. .probe = wm0010_probe,
  668. .set_bias_level = wm0010_set_bias_level,
  669. .set_sysclk = wm0010_set_sysclk,
  670. .idle_bias_off = true,
  671. .dapm_widgets = wm0010_dapm_widgets,
  672. .num_dapm_widgets = ARRAY_SIZE(wm0010_dapm_widgets),
  673. .dapm_routes = wm0010_dapm_routes,
  674. .num_dapm_routes = ARRAY_SIZE(wm0010_dapm_routes),
  675. };
  676. #define WM0010_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
  677. #define WM0010_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE |\
  678. SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE |\
  679. SNDRV_PCM_FMTBIT_S32_LE)
  680. static struct snd_soc_dai_driver wm0010_dai[] = {
  681. {
  682. .name = "wm0010-sdi1",
  683. .playback = {
  684. .stream_name = "SDI1 Playback",
  685. .channels_min = 1,
  686. .channels_max = 2,
  687. .rates = WM0010_RATES,
  688. .formats = WM0010_FORMATS,
  689. },
  690. .capture = {
  691. .stream_name = "SDI1 Capture",
  692. .channels_min = 1,
  693. .channels_max = 2,
  694. .rates = WM0010_RATES,
  695. .formats = WM0010_FORMATS,
  696. },
  697. },
  698. {
  699. .name = "wm0010-sdi2",
  700. .playback = {
  701. .stream_name = "SDI2 Playback",
  702. .channels_min = 1,
  703. .channels_max = 2,
  704. .rates = WM0010_RATES,
  705. .formats = WM0010_FORMATS,
  706. },
  707. .capture = {
  708. .stream_name = "SDI2 Capture",
  709. .channels_min = 1,
  710. .channels_max = 2,
  711. .rates = WM0010_RATES,
  712. .formats = WM0010_FORMATS,
  713. },
  714. },
  715. };
  716. static irqreturn_t wm0010_irq(int irq, void *data)
  717. {
  718. struct wm0010_priv *wm0010 = data;
  719. switch (wm0010->state) {
  720. case WM0010_OUT_OF_RESET:
  721. case WM0010_BOOTROM:
  722. case WM0010_STAGE2:
  723. spin_lock(&wm0010->irq_lock);
  724. complete(&wm0010->boot_completion);
  725. spin_unlock(&wm0010->irq_lock);
  726. return IRQ_HANDLED;
  727. default:
  728. return IRQ_NONE;
  729. }
  730. return IRQ_NONE;
  731. }
  732. static int wm0010_probe(struct snd_soc_codec *codec)
  733. {
  734. struct wm0010_priv *wm0010 = snd_soc_codec_get_drvdata(codec);
  735. wm0010->codec = codec;
  736. return 0;
  737. }
  738. static int wm0010_spi_probe(struct spi_device *spi)
  739. {
  740. unsigned long gpio_flags;
  741. int ret;
  742. int trigger;
  743. int irq;
  744. struct wm0010_priv *wm0010;
  745. wm0010 = devm_kzalloc(&spi->dev, sizeof(*wm0010),
  746. GFP_KERNEL);
  747. if (!wm0010)
  748. return -ENOMEM;
  749. mutex_init(&wm0010->lock);
  750. spin_lock_init(&wm0010->irq_lock);
  751. spi_set_drvdata(spi, wm0010);
  752. wm0010->dev = &spi->dev;
  753. if (dev_get_platdata(&spi->dev))
  754. memcpy(&wm0010->pdata, dev_get_platdata(&spi->dev),
  755. sizeof(wm0010->pdata));
  756. init_completion(&wm0010->boot_completion);
  757. wm0010->core_supplies[0].supply = "AVDD";
  758. wm0010->core_supplies[1].supply = "DCVDD";
  759. ret = devm_regulator_bulk_get(wm0010->dev, ARRAY_SIZE(wm0010->core_supplies),
  760. wm0010->core_supplies);
  761. if (ret != 0) {
  762. dev_err(wm0010->dev, "Failed to obtain core supplies: %d\n",
  763. ret);
  764. return ret;
  765. }
  766. wm0010->dbvdd = devm_regulator_get(wm0010->dev, "DBVDD");
  767. if (IS_ERR(wm0010->dbvdd)) {
  768. ret = PTR_ERR(wm0010->dbvdd);
  769. dev_err(wm0010->dev, "Failed to obtain DBVDD: %d\n", ret);
  770. return ret;
  771. }
  772. if (wm0010->pdata.gpio_reset) {
  773. wm0010->gpio_reset = wm0010->pdata.gpio_reset;
  774. if (wm0010->pdata.reset_active_high)
  775. wm0010->gpio_reset_value = 1;
  776. else
  777. wm0010->gpio_reset_value = 0;
  778. if (wm0010->gpio_reset_value)
  779. gpio_flags = GPIOF_OUT_INIT_HIGH;
  780. else
  781. gpio_flags = GPIOF_OUT_INIT_LOW;
  782. ret = devm_gpio_request_one(wm0010->dev, wm0010->gpio_reset,
  783. gpio_flags, "wm0010 reset");
  784. if (ret < 0) {
  785. dev_err(wm0010->dev,
  786. "Failed to request GPIO for DSP reset: %d\n",
  787. ret);
  788. return ret;
  789. }
  790. } else {
  791. dev_err(wm0010->dev, "No reset GPIO configured\n");
  792. return -EINVAL;
  793. }
  794. wm0010->state = WM0010_POWER_OFF;
  795. irq = spi->irq;
  796. if (wm0010->pdata.irq_flags)
  797. trigger = wm0010->pdata.irq_flags;
  798. else
  799. trigger = IRQF_TRIGGER_FALLING;
  800. trigger |= IRQF_ONESHOT;
  801. ret = request_threaded_irq(irq, NULL, wm0010_irq, trigger | IRQF_ONESHOT,
  802. "wm0010", wm0010);
  803. if (ret) {
  804. dev_err(wm0010->dev, "Failed to request IRQ %d: %d\n",
  805. irq, ret);
  806. return ret;
  807. }
  808. wm0010->irq = irq;
  809. ret = irq_set_irq_wake(irq, 1);
  810. if (ret) {
  811. dev_err(wm0010->dev, "Failed to set IRQ %d as wake source: %d\n",
  812. irq, ret);
  813. return ret;
  814. }
  815. if (spi->max_speed_hz)
  816. wm0010->board_max_spi_speed = spi->max_speed_hz;
  817. else
  818. wm0010->board_max_spi_speed = 0;
  819. ret = snd_soc_register_codec(&spi->dev,
  820. &soc_codec_dev_wm0010, wm0010_dai,
  821. ARRAY_SIZE(wm0010_dai));
  822. if (ret < 0)
  823. return ret;
  824. return 0;
  825. }
  826. static int wm0010_spi_remove(struct spi_device *spi)
  827. {
  828. struct wm0010_priv *wm0010 = spi_get_drvdata(spi);
  829. snd_soc_unregister_codec(&spi->dev);
  830. gpio_set_value_cansleep(wm0010->gpio_reset,
  831. wm0010->gpio_reset_value);
  832. irq_set_irq_wake(wm0010->irq, 0);
  833. if (wm0010->irq)
  834. free_irq(wm0010->irq, wm0010);
  835. return 0;
  836. }
  837. static struct spi_driver wm0010_spi_driver = {
  838. .driver = {
  839. .name = "wm0010",
  840. .bus = &spi_bus_type,
  841. .owner = THIS_MODULE,
  842. },
  843. .probe = wm0010_spi_probe,
  844. .remove = wm0010_spi_remove,
  845. };
  846. module_spi_driver(wm0010_spi_driver);
  847. MODULE_DESCRIPTION("ASoC WM0010 driver");
  848. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  849. MODULE_LICENSE("GPL");