wm0010.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  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. bool ready;
  86. bool pll_running;
  87. int max_spi_freq;
  88. int board_max_spi_speed;
  89. u32 pll_clkctrl1;
  90. spinlock_t irq_lock;
  91. int irq;
  92. struct completion boot_completion;
  93. };
  94. struct wm0010_spi_msg {
  95. struct spi_message m;
  96. struct spi_transfer t;
  97. u8 *tx_buf;
  98. u8 *rx_buf;
  99. size_t len;
  100. };
  101. static const struct snd_soc_dapm_widget wm0010_dapm_widgets[] = {
  102. SND_SOC_DAPM_SUPPLY("CLKIN", SND_SOC_NOPM, 0, 0, NULL, 0),
  103. };
  104. static const struct snd_soc_dapm_route wm0010_dapm_routes[] = {
  105. { "SDI2 Capture", NULL, "SDI1 Playback" },
  106. { "SDI1 Capture", NULL, "SDI2 Playback" },
  107. { "SDI1 Capture", NULL, "CLKIN" },
  108. { "SDI2 Capture", NULL, "CLKIN" },
  109. { "SDI1 Playback", NULL, "CLKIN" },
  110. { "SDI2 Playback", NULL, "CLKIN" },
  111. };
  112. static const char *wm0010_state_to_str(enum wm0010_state state)
  113. {
  114. const char *state_to_str[] = {
  115. "Power off",
  116. "Out of reset",
  117. "Boot ROM",
  118. "Stage2",
  119. "Firmware"
  120. };
  121. if (state < 0 || state >= ARRAY_SIZE(state_to_str))
  122. return "null";
  123. return state_to_str[state];
  124. }
  125. /* Called with wm0010->lock held */
  126. static void wm0010_halt(struct snd_soc_codec *codec)
  127. {
  128. struct wm0010_priv *wm0010 = snd_soc_codec_get_drvdata(codec);
  129. unsigned long flags;
  130. enum wm0010_state state;
  131. /* Fetch the wm0010 state */
  132. spin_lock_irqsave(&wm0010->irq_lock, flags);
  133. state = wm0010->state;
  134. spin_unlock_irqrestore(&wm0010->irq_lock, flags);
  135. switch (state) {
  136. case WM0010_POWER_OFF:
  137. /* If there's nothing to do, bail out */
  138. return;
  139. case WM0010_OUT_OF_RESET:
  140. case WM0010_BOOTROM:
  141. case WM0010_STAGE2:
  142. case WM0010_FIRMWARE:
  143. /* Remember to put chip back into reset */
  144. gpio_set_value_cansleep(wm0010->gpio_reset,
  145. 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->state < WM0010_STAGE2)
  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. if (xfer->done)
  269. complete(xfer->done);
  270. }
  271. static void byte_swap_64(u64 *data_in, u64 *data_out, u32 len)
  272. {
  273. int i;
  274. for (i = 0; i < len / 8; i++)
  275. data_out[i] = cpu_to_be64(le64_to_cpu(data_in[i]));
  276. }
  277. static int wm0010_boot(struct snd_soc_codec *codec)
  278. {
  279. struct spi_device *spi = to_spi_device(codec->dev);
  280. struct wm0010_priv *wm0010 = snd_soc_codec_get_drvdata(codec);
  281. unsigned long flags;
  282. struct list_head xfer_list;
  283. struct wm0010_boot_xfer *xfer;
  284. int ret;
  285. struct completion done;
  286. const struct firmware *fw;
  287. const struct dfw_binrec *rec;
  288. struct spi_message m;
  289. struct spi_transfer t;
  290. struct dfw_pllrec pll_rec;
  291. u32 *img, *p;
  292. u64 *img_swap;
  293. u8 *out;
  294. u32 len, offset;
  295. int i;
  296. spin_lock_irqsave(&wm0010->irq_lock, flags);
  297. if (wm0010->state != WM0010_POWER_OFF)
  298. dev_warn(wm0010->dev, "DSP already powered up!\n");
  299. spin_unlock_irqrestore(&wm0010->irq_lock, flags);
  300. if (wm0010->sysclk > 26000000) {
  301. dev_err(codec->dev, "Max DSP clock frequency is 26MHz\n");
  302. ret = -ECANCELED;
  303. goto err;
  304. }
  305. INIT_LIST_HEAD(&xfer_list);
  306. mutex_lock(&wm0010->lock);
  307. wm0010->pll_running = false;
  308. dev_dbg(codec->dev, "max_spi_freq: %d\n", wm0010->max_spi_freq);
  309. ret = regulator_bulk_enable(ARRAY_SIZE(wm0010->core_supplies),
  310. wm0010->core_supplies);
  311. if (ret != 0) {
  312. dev_err(&spi->dev, "Failed to enable core supplies: %d\n",
  313. ret);
  314. mutex_unlock(&wm0010->lock);
  315. goto err;
  316. }
  317. ret = regulator_enable(wm0010->dbvdd);
  318. if (ret != 0) {
  319. dev_err(&spi->dev, "Failed to enable DBVDD: %d\n", ret);
  320. goto err_core;
  321. }
  322. /* Release reset */
  323. gpio_set_value_cansleep(wm0010->gpio_reset, !wm0010->gpio_reset_value);
  324. spin_lock_irqsave(&wm0010->irq_lock, flags);
  325. wm0010->state = WM0010_OUT_OF_RESET;
  326. spin_unlock_irqrestore(&wm0010->irq_lock, flags);
  327. /* First the bootloader */
  328. ret = request_firmware(&fw, "wm0010_stage2.bin", codec->dev);
  329. if (ret != 0) {
  330. dev_err(codec->dev, "Failed to request stage2 loader: %d\n",
  331. ret);
  332. goto abort;
  333. }
  334. if (!wait_for_completion_timeout(&wm0010->boot_completion,
  335. msecs_to_jiffies(10)))
  336. dev_err(codec->dev, "Failed to get interrupt from DSP\n");
  337. spin_lock_irqsave(&wm0010->irq_lock, flags);
  338. wm0010->state = WM0010_BOOTROM;
  339. spin_unlock_irqrestore(&wm0010->irq_lock, flags);
  340. dev_dbg(codec->dev, "Downloading %zu byte stage 2 loader\n", fw->size);
  341. /* Copy to local buffer first as vmalloc causes problems for dma */
  342. img = kzalloc(fw->size, GFP_KERNEL);
  343. if (!img) {
  344. dev_err(codec->dev, "Failed to allocate image buffer\n");
  345. goto abort;
  346. }
  347. out = kzalloc(fw->size, GFP_KERNEL);
  348. if (!out) {
  349. dev_err(codec->dev, "Failed to allocate output buffer\n");
  350. goto abort;
  351. }
  352. memcpy(img, &fw->data[0], fw->size);
  353. spi_message_init(&m);
  354. memset(&t, 0, sizeof(t));
  355. t.rx_buf = out;
  356. t.tx_buf = img;
  357. t.len = fw->size;
  358. t.bits_per_word = 8;
  359. t.speed_hz = wm0010->sysclk / 10;
  360. spi_message_add_tail(&t, &m);
  361. dev_dbg(codec->dev, "Starting initial download at %dHz\n",
  362. t.speed_hz);
  363. ret = spi_sync(spi, &m);
  364. if (ret != 0) {
  365. dev_err(codec->dev, "Initial download failed: %d\n", ret);
  366. goto abort;
  367. }
  368. /* Look for errors from the boot ROM */
  369. for (i = 0; i < fw->size; i++) {
  370. if (out[i] != 0x55) {
  371. ret = -EBUSY;
  372. dev_err(codec->dev, "Boot ROM error: %x in %d\n",
  373. out[i], i);
  374. wm0010_mark_boot_failure(wm0010);
  375. goto abort;
  376. }
  377. }
  378. release_firmware(fw);
  379. kfree(img);
  380. kfree(out);
  381. if (!wait_for_completion_timeout(&wm0010->boot_completion,
  382. msecs_to_jiffies(10)))
  383. dev_err(codec->dev, "Failed to get interrupt from DSP loader.\n");
  384. spin_lock_irqsave(&wm0010->irq_lock, flags);
  385. wm0010->state = WM0010_STAGE2;
  386. spin_unlock_irqrestore(&wm0010->irq_lock, flags);
  387. /* Only initialise PLL if max_spi_freq initialised */
  388. if (wm0010->max_spi_freq) {
  389. /* Initialise a PLL record */
  390. memset(&pll_rec, 0, sizeof(pll_rec));
  391. pll_rec.command = DFW_CMD_PLL;
  392. pll_rec.length = (sizeof(pll_rec) - 8);
  393. /* On wm0010 only the CLKCTRL1 value is used */
  394. pll_rec.clkctrl1 = wm0010->pll_clkctrl1;
  395. len = pll_rec.length + 8;
  396. out = kzalloc(len, GFP_KERNEL);
  397. if (!out) {
  398. dev_err(codec->dev,
  399. "Failed to allocate RX buffer\n");
  400. goto abort;
  401. }
  402. img_swap = kzalloc(len, GFP_KERNEL);
  403. if (!img_swap) {
  404. dev_err(codec->dev,
  405. "Failed to allocate image buffer\n");
  406. goto abort;
  407. }
  408. /* We need to re-order for 0010 */
  409. byte_swap_64((u64 *)&pll_rec, img_swap, len);
  410. spi_message_init(&m);
  411. memset(&t, 0, sizeof(t));
  412. t.rx_buf = out;
  413. t.tx_buf = img_swap;
  414. t.len = len;
  415. t.bits_per_word = 8;
  416. t.speed_hz = wm0010->sysclk / 6;
  417. spi_message_add_tail(&t, &m);
  418. ret = spi_sync(spi, &m);
  419. if (ret != 0) {
  420. dev_err(codec->dev, "First PLL write failed: %d\n", ret);
  421. goto abort;
  422. }
  423. /* Use a second send of the message to get the return status */
  424. ret = spi_sync(spi, &m);
  425. if (ret != 0) {
  426. dev_err(codec->dev, "Second PLL write failed: %d\n", ret);
  427. goto abort;
  428. }
  429. p = (u32 *)out;
  430. /* Look for PLL active code from the DSP */
  431. for (i = 0; i < len / 4; i++) {
  432. if (*p == 0x0e00ed0f) {
  433. dev_dbg(codec->dev, "PLL packet received\n");
  434. wm0010->pll_running = true;
  435. break;
  436. }
  437. p++;
  438. }
  439. kfree(img_swap);
  440. kfree(out);
  441. } else
  442. dev_dbg(codec->dev, "Not enabling DSP PLL.");
  443. ret = request_firmware(&fw, "wm0010.dfw", codec->dev);
  444. if (ret != 0) {
  445. dev_err(codec->dev, "Failed to request application: %d\n",
  446. ret);
  447. goto abort;
  448. }
  449. rec = (const struct dfw_binrec *)fw->data;
  450. offset = 0;
  451. wm0010->boot_failed = false;
  452. BUG_ON(!list_empty(&xfer_list));
  453. init_completion(&done);
  454. /* First record should be INFO */
  455. if (rec->command != DFW_CMD_INFO) {
  456. dev_err(codec->dev, "First record not INFO\r\n");
  457. goto abort;
  458. }
  459. /* Check it's a 0010 file */
  460. if (rec->data[0] != DEVICE_ID_WM0010) {
  461. dev_err(codec->dev, "Not a WM0010 firmware file.\r\n");
  462. goto abort;
  463. }
  464. /* Skip the info record as we don't need to send it */
  465. offset += ((rec->length) + 8);
  466. rec = (void *)&rec->data[rec->length];
  467. while (offset < fw->size) {
  468. dev_dbg(codec->dev,
  469. "Packet: command %d, data length = 0x%x\r\n",
  470. rec->command, rec->length);
  471. len = rec->length + 8;
  472. out = kzalloc(len, GFP_KERNEL);
  473. if (!out) {
  474. dev_err(codec->dev,
  475. "Failed to allocate RX buffer\n");
  476. goto abort;
  477. }
  478. img_swap = kzalloc(len, GFP_KERNEL);
  479. if (!img_swap) {
  480. dev_err(codec->dev,
  481. "Failed to allocate image buffer\n");
  482. goto abort;
  483. }
  484. /* We need to re-order for 0010 */
  485. byte_swap_64((u64 *)&rec->command, img_swap, len);
  486. xfer = kzalloc(sizeof(*xfer), GFP_KERNEL);
  487. if (!xfer) {
  488. dev_err(codec->dev, "Failed to allocate xfer\n");
  489. goto abort;
  490. }
  491. xfer->codec = codec;
  492. list_add_tail(&xfer->list, &xfer_list);
  493. spi_message_init(&xfer->m);
  494. xfer->m.complete = wm0010_boot_xfer_complete;
  495. xfer->m.context = xfer;
  496. xfer->t.tx_buf = img_swap;
  497. xfer->t.rx_buf = out;
  498. xfer->t.len = len;
  499. xfer->t.bits_per_word = 8;
  500. if (!wm0010->pll_running) {
  501. xfer->t.speed_hz = wm0010->sysclk / 6;
  502. } else {
  503. xfer->t.speed_hz = wm0010->max_spi_freq;
  504. if (wm0010->board_max_spi_speed &&
  505. (wm0010->board_max_spi_speed < wm0010->max_spi_freq))
  506. xfer->t.speed_hz = wm0010->board_max_spi_speed;
  507. }
  508. /* Store max usable spi frequency for later use */
  509. wm0010->max_spi_freq = xfer->t.speed_hz;
  510. spi_message_add_tail(&xfer->t, &xfer->m);
  511. offset += ((rec->length) + 8);
  512. rec = (void *)&rec->data[rec->length];
  513. if (offset >= fw->size) {
  514. dev_dbg(codec->dev, "All transfers scheduled\n");
  515. xfer->done = &done;
  516. }
  517. ret = spi_async(spi, &xfer->m);
  518. if (ret != 0) {
  519. dev_err(codec->dev, "Write failed: %d\n", ret);
  520. goto abort;
  521. }
  522. if (wm0010->boot_failed)
  523. goto abort;
  524. }
  525. wait_for_completion(&done);
  526. spin_lock_irqsave(&wm0010->irq_lock, flags);
  527. wm0010->state = WM0010_FIRMWARE;
  528. spin_unlock_irqrestore(&wm0010->irq_lock, flags);
  529. mutex_unlock(&wm0010->lock);
  530. release_firmware(fw);
  531. while (!list_empty(&xfer_list)) {
  532. xfer = list_first_entry(&xfer_list, struct wm0010_boot_xfer,
  533. list);
  534. kfree(xfer->t.rx_buf);
  535. kfree(xfer->t.tx_buf);
  536. list_del(&xfer->list);
  537. kfree(xfer);
  538. }
  539. return 0;
  540. abort:
  541. /* Put the chip back into reset */
  542. wm0010_halt(codec);
  543. mutex_unlock(&wm0010->lock);
  544. return ret;
  545. err_core:
  546. mutex_unlock(&wm0010->lock);
  547. regulator_bulk_disable(ARRAY_SIZE(wm0010->core_supplies),
  548. wm0010->core_supplies);
  549. err:
  550. return ret;
  551. }
  552. static int wm0010_set_bias_level(struct snd_soc_codec *codec,
  553. enum snd_soc_bias_level level)
  554. {
  555. struct wm0010_priv *wm0010 = snd_soc_codec_get_drvdata(codec);
  556. switch (level) {
  557. case SND_SOC_BIAS_ON:
  558. if (codec->dapm.bias_level == SND_SOC_BIAS_PREPARE)
  559. wm0010_boot(codec);
  560. break;
  561. case SND_SOC_BIAS_PREPARE:
  562. break;
  563. case SND_SOC_BIAS_STANDBY:
  564. if (codec->dapm.bias_level == SND_SOC_BIAS_PREPARE) {
  565. mutex_lock(&wm0010->lock);
  566. wm0010_halt(codec);
  567. mutex_unlock(&wm0010->lock);
  568. }
  569. break;
  570. case SND_SOC_BIAS_OFF:
  571. break;
  572. }
  573. codec->dapm.bias_level = level;
  574. return 0;
  575. }
  576. static int wm0010_set_sysclk(struct snd_soc_codec *codec, int source,
  577. int clk_id, unsigned int freq, int dir)
  578. {
  579. struct wm0010_priv *wm0010 = snd_soc_codec_get_drvdata(codec);
  580. unsigned int i;
  581. wm0010->sysclk = freq;
  582. if (freq < pll_clock_map[ARRAY_SIZE(pll_clock_map)-1].max_sysclk) {
  583. wm0010->max_spi_freq = 0;
  584. } else {
  585. for (i = 0; i < ARRAY_SIZE(pll_clock_map); i++)
  586. if (freq >= pll_clock_map[i].max_sysclk)
  587. break;
  588. wm0010->max_spi_freq = pll_clock_map[i].max_pll_spi_speed;
  589. wm0010->pll_clkctrl1 = pll_clock_map[i].pll_clkctrl1;
  590. }
  591. return 0;
  592. }
  593. static int wm0010_probe(struct snd_soc_codec *codec);
  594. static struct snd_soc_codec_driver soc_codec_dev_wm0010 = {
  595. .probe = wm0010_probe,
  596. .set_bias_level = wm0010_set_bias_level,
  597. .set_sysclk = wm0010_set_sysclk,
  598. .idle_bias_off = true,
  599. .dapm_widgets = wm0010_dapm_widgets,
  600. .num_dapm_widgets = ARRAY_SIZE(wm0010_dapm_widgets),
  601. .dapm_routes = wm0010_dapm_routes,
  602. .num_dapm_routes = ARRAY_SIZE(wm0010_dapm_routes),
  603. };
  604. #define WM0010_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
  605. #define WM0010_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE |\
  606. SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE |\
  607. SNDRV_PCM_FMTBIT_S32_LE)
  608. static struct snd_soc_dai_driver wm0010_dai[] = {
  609. {
  610. .name = "wm0010-sdi1",
  611. .playback = {
  612. .stream_name = "SDI1 Playback",
  613. .channels_min = 1,
  614. .channels_max = 2,
  615. .rates = WM0010_RATES,
  616. .formats = WM0010_FORMATS,
  617. },
  618. .capture = {
  619. .stream_name = "SDI1 Capture",
  620. .channels_min = 1,
  621. .channels_max = 2,
  622. .rates = WM0010_RATES,
  623. .formats = WM0010_FORMATS,
  624. },
  625. },
  626. {
  627. .name = "wm0010-sdi2",
  628. .playback = {
  629. .stream_name = "SDI2 Playback",
  630. .channels_min = 1,
  631. .channels_max = 2,
  632. .rates = WM0010_RATES,
  633. .formats = WM0010_FORMATS,
  634. },
  635. .capture = {
  636. .stream_name = "SDI2 Capture",
  637. .channels_min = 1,
  638. .channels_max = 2,
  639. .rates = WM0010_RATES,
  640. .formats = WM0010_FORMATS,
  641. },
  642. },
  643. };
  644. static irqreturn_t wm0010_irq(int irq, void *data)
  645. {
  646. struct wm0010_priv *wm0010 = data;
  647. switch (wm0010->state) {
  648. case WM0010_POWER_OFF:
  649. case WM0010_OUT_OF_RESET:
  650. case WM0010_BOOTROM:
  651. case WM0010_STAGE2:
  652. spin_lock(&wm0010->irq_lock);
  653. complete(&wm0010->boot_completion);
  654. spin_unlock(&wm0010->irq_lock);
  655. return IRQ_HANDLED;
  656. default:
  657. return IRQ_NONE;
  658. }
  659. return IRQ_NONE;
  660. }
  661. static int wm0010_probe(struct snd_soc_codec *codec)
  662. {
  663. struct wm0010_priv *wm0010 = snd_soc_codec_get_drvdata(codec);
  664. wm0010->codec = codec;
  665. return 0;
  666. }
  667. static int __devinit wm0010_spi_probe(struct spi_device *spi)
  668. {
  669. unsigned long gpio_flags;
  670. int ret;
  671. int trigger;
  672. int irq;
  673. struct wm0010_priv *wm0010;
  674. wm0010 = devm_kzalloc(&spi->dev, sizeof(*wm0010),
  675. GFP_KERNEL);
  676. if (!wm0010)
  677. return -ENOMEM;
  678. mutex_init(&wm0010->lock);
  679. spin_lock_init(&wm0010->irq_lock);
  680. spi_set_drvdata(spi, wm0010);
  681. wm0010->dev = &spi->dev;
  682. if (dev_get_platdata(&spi->dev))
  683. memcpy(&wm0010->pdata, dev_get_platdata(&spi->dev),
  684. sizeof(wm0010->pdata));
  685. init_completion(&wm0010->boot_completion);
  686. wm0010->core_supplies[0].supply = "AVDD";
  687. wm0010->core_supplies[1].supply = "DCVDD";
  688. ret = devm_regulator_bulk_get(wm0010->dev, ARRAY_SIZE(wm0010->core_supplies),
  689. wm0010->core_supplies);
  690. if (ret != 0) {
  691. dev_err(wm0010->dev, "Failed to obtain core supplies: %d\n",
  692. ret);
  693. return ret;
  694. }
  695. wm0010->dbvdd = devm_regulator_get(wm0010->dev, "DBVDD");
  696. if (IS_ERR(wm0010->dbvdd)) {
  697. ret = PTR_ERR(wm0010->dbvdd);
  698. dev_err(wm0010->dev, "Failed to obtain DBVDD: %d\n", ret);
  699. return ret;
  700. }
  701. if (wm0010->pdata.gpio_reset) {
  702. wm0010->gpio_reset = wm0010->pdata.gpio_reset;
  703. if (wm0010->pdata.reset_active_high)
  704. wm0010->gpio_reset_value = 1;
  705. else
  706. wm0010->gpio_reset_value = 0;
  707. if (wm0010->gpio_reset_value)
  708. gpio_flags = GPIOF_OUT_INIT_HIGH;
  709. else
  710. gpio_flags = GPIOF_OUT_INIT_LOW;
  711. ret = devm_gpio_request_one(wm0010->dev, wm0010->gpio_reset,
  712. gpio_flags, "wm0010 reset");
  713. if (ret < 0) {
  714. dev_err(wm0010->dev,
  715. "Failed to request GPIO for DSP reset: %d\n",
  716. ret);
  717. return ret;
  718. }
  719. } else {
  720. dev_err(wm0010->dev, "No reset GPIO configured\n");
  721. return -EINVAL;
  722. }
  723. wm0010->state = WM0010_POWER_OFF;
  724. irq = spi->irq;
  725. if (wm0010->pdata.irq_flags)
  726. trigger = wm0010->pdata.irq_flags;
  727. else
  728. trigger = IRQF_TRIGGER_FALLING;
  729. trigger |= IRQF_ONESHOT;
  730. ret = request_threaded_irq(irq, NULL, wm0010_irq, trigger | IRQF_ONESHOT,
  731. "wm0010", wm0010);
  732. if (ret) {
  733. dev_err(wm0010->dev, "Failed to request IRQ %d: %d\n",
  734. irq, ret);
  735. return ret;
  736. }
  737. wm0010->irq = irq;
  738. if (spi->max_speed_hz)
  739. wm0010->board_max_spi_speed = spi->max_speed_hz;
  740. else
  741. wm0010->board_max_spi_speed = 0;
  742. ret = snd_soc_register_codec(&spi->dev,
  743. &soc_codec_dev_wm0010, wm0010_dai,
  744. ARRAY_SIZE(wm0010_dai));
  745. if (ret < 0)
  746. return ret;
  747. return 0;
  748. }
  749. static int __devexit wm0010_spi_remove(struct spi_device *spi)
  750. {
  751. struct wm0010_priv *wm0010 = spi_get_drvdata(spi);
  752. snd_soc_unregister_codec(&spi->dev);
  753. gpio_set_value_cansleep(wm0010->gpio_reset,
  754. wm0010->gpio_reset_value);
  755. if (wm0010->irq)
  756. free_irq(wm0010->irq, wm0010);
  757. return 0;
  758. }
  759. static struct spi_driver wm0010_spi_driver = {
  760. .driver = {
  761. .name = "wm0010",
  762. .bus = &spi_bus_type,
  763. .owner = THIS_MODULE,
  764. },
  765. .probe = wm0010_spi_probe,
  766. .remove = __devexit_p(wm0010_spi_remove),
  767. };
  768. module_spi_driver(wm0010_spi_driver);
  769. MODULE_DESCRIPTION("ASoC WM0010 driver");
  770. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  771. MODULE_LICENSE("GPL");