wm0010.c 23 KB

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