wm0010.c 22 KB

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