wm0010.c 22 KB

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