wm0010.c 23 KB

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