wm0010.c 21 KB

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