wm0010.c 23 KB

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