wm0010.c 21 KB

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