wm8741.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * wm8741.c -- WM8741 ALSA SoC Audio driver
  4. *
  5. * Copyright 2010-1 Wolfson Microelectronics plc
  6. *
  7. * Author: Ian Lartey <[email protected]>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/moduleparam.h>
  11. #include <linux/init.h>
  12. #include <linux/delay.h>
  13. #include <linux/pm.h>
  14. #include <linux/i2c.h>
  15. #include <linux/spi/spi.h>
  16. #include <linux/regmap.h>
  17. #include <linux/regulator/consumer.h>
  18. #include <linux/slab.h>
  19. #include <linux/of_device.h>
  20. #include <sound/core.h>
  21. #include <sound/pcm.h>
  22. #include <sound/pcm_params.h>
  23. #include <sound/soc.h>
  24. #include <sound/initval.h>
  25. #include <sound/tlv.h>
  26. #include "wm8741.h"
  27. #define WM8741_NUM_SUPPLIES 2
  28. static const char *wm8741_supply_names[WM8741_NUM_SUPPLIES] = {
  29. "AVDD",
  30. "DVDD",
  31. };
  32. /* codec private data */
  33. struct wm8741_priv {
  34. struct wm8741_platform_data pdata;
  35. struct regmap *regmap;
  36. struct regulator_bulk_data supplies[WM8741_NUM_SUPPLIES];
  37. unsigned int sysclk;
  38. const struct snd_pcm_hw_constraint_list *sysclk_constraints;
  39. };
  40. static const struct reg_default wm8741_reg_defaults[] = {
  41. { 0, 0x0000 }, /* R0 - DACLLSB Attenuation */
  42. { 1, 0x0000 }, /* R1 - DACLMSB Attenuation */
  43. { 2, 0x0000 }, /* R2 - DACRLSB Attenuation */
  44. { 3, 0x0000 }, /* R3 - DACRMSB Attenuation */
  45. { 4, 0x0000 }, /* R4 - Volume Control */
  46. { 5, 0x000A }, /* R5 - Format Control */
  47. { 6, 0x0000 }, /* R6 - Filter Control */
  48. { 7, 0x0000 }, /* R7 - Mode Control 1 */
  49. { 8, 0x0002 }, /* R8 - Mode Control 2 */
  50. { 32, 0x0002 }, /* R32 - ADDITONAL_CONTROL_1 */
  51. };
  52. static int wm8741_reset(struct snd_soc_component *component)
  53. {
  54. return snd_soc_component_write(component, WM8741_RESET, 0);
  55. }
  56. static const DECLARE_TLV_DB_SCALE(dac_tlv_fine, -12700, 13, 0);
  57. static const DECLARE_TLV_DB_SCALE(dac_tlv, -12700, 400, 0);
  58. static const struct snd_kcontrol_new wm8741_snd_controls_stereo[] = {
  59. SOC_DOUBLE_R_TLV("Fine Playback Volume", WM8741_DACLLSB_ATTENUATION,
  60. WM8741_DACRLSB_ATTENUATION, 1, 255, 1, dac_tlv_fine),
  61. SOC_DOUBLE_R_TLV("Playback Volume", WM8741_DACLMSB_ATTENUATION,
  62. WM8741_DACRMSB_ATTENUATION, 0, 511, 1, dac_tlv),
  63. };
  64. static const struct snd_kcontrol_new wm8741_snd_controls_mono_left[] = {
  65. SOC_SINGLE_TLV("Fine Playback Volume", WM8741_DACLLSB_ATTENUATION,
  66. 1, 255, 1, dac_tlv_fine),
  67. SOC_SINGLE_TLV("Playback Volume", WM8741_DACLMSB_ATTENUATION,
  68. 0, 511, 1, dac_tlv),
  69. };
  70. static const struct snd_kcontrol_new wm8741_snd_controls_mono_right[] = {
  71. SOC_SINGLE_TLV("Fine Playback Volume", WM8741_DACRLSB_ATTENUATION,
  72. 1, 255, 1, dac_tlv_fine),
  73. SOC_SINGLE_TLV("Playback Volume", WM8741_DACRMSB_ATTENUATION,
  74. 0, 511, 1, dac_tlv),
  75. };
  76. static const struct snd_soc_dapm_widget wm8741_dapm_widgets[] = {
  77. SND_SOC_DAPM_DAC("DACL", "Playback", SND_SOC_NOPM, 0, 0),
  78. SND_SOC_DAPM_DAC("DACR", "Playback", SND_SOC_NOPM, 0, 0),
  79. SND_SOC_DAPM_OUTPUT("VOUTLP"),
  80. SND_SOC_DAPM_OUTPUT("VOUTLN"),
  81. SND_SOC_DAPM_OUTPUT("VOUTRP"),
  82. SND_SOC_DAPM_OUTPUT("VOUTRN"),
  83. };
  84. static const struct snd_soc_dapm_route wm8741_dapm_routes[] = {
  85. { "VOUTLP", NULL, "DACL" },
  86. { "VOUTLN", NULL, "DACL" },
  87. { "VOUTRP", NULL, "DACR" },
  88. { "VOUTRN", NULL, "DACR" },
  89. };
  90. static const unsigned int rates_11289[] = {
  91. 44100, 88200,
  92. };
  93. static const struct snd_pcm_hw_constraint_list constraints_11289 = {
  94. .count = ARRAY_SIZE(rates_11289),
  95. .list = rates_11289,
  96. };
  97. static const unsigned int rates_12288[] = {
  98. 32000, 48000, 96000,
  99. };
  100. static const struct snd_pcm_hw_constraint_list constraints_12288 = {
  101. .count = ARRAY_SIZE(rates_12288),
  102. .list = rates_12288,
  103. };
  104. static const unsigned int rates_16384[] = {
  105. 32000,
  106. };
  107. static const struct snd_pcm_hw_constraint_list constraints_16384 = {
  108. .count = ARRAY_SIZE(rates_16384),
  109. .list = rates_16384,
  110. };
  111. static const unsigned int rates_16934[] = {
  112. 44100, 88200,
  113. };
  114. static const struct snd_pcm_hw_constraint_list constraints_16934 = {
  115. .count = ARRAY_SIZE(rates_16934),
  116. .list = rates_16934,
  117. };
  118. static const unsigned int rates_18432[] = {
  119. 48000, 96000,
  120. };
  121. static const struct snd_pcm_hw_constraint_list constraints_18432 = {
  122. .count = ARRAY_SIZE(rates_18432),
  123. .list = rates_18432,
  124. };
  125. static const unsigned int rates_22579[] = {
  126. 44100, 88200, 176400
  127. };
  128. static const struct snd_pcm_hw_constraint_list constraints_22579 = {
  129. .count = ARRAY_SIZE(rates_22579),
  130. .list = rates_22579,
  131. };
  132. static const unsigned int rates_24576[] = {
  133. 32000, 48000, 96000, 192000
  134. };
  135. static const struct snd_pcm_hw_constraint_list constraints_24576 = {
  136. .count = ARRAY_SIZE(rates_24576),
  137. .list = rates_24576,
  138. };
  139. static const unsigned int rates_36864[] = {
  140. 48000, 96000, 192000
  141. };
  142. static const struct snd_pcm_hw_constraint_list constraints_36864 = {
  143. .count = ARRAY_SIZE(rates_36864),
  144. .list = rates_36864,
  145. };
  146. static int wm8741_startup(struct snd_pcm_substream *substream,
  147. struct snd_soc_dai *dai)
  148. {
  149. struct snd_soc_component *component = dai->component;
  150. struct wm8741_priv *wm8741 = snd_soc_component_get_drvdata(component);
  151. if (wm8741->sysclk)
  152. snd_pcm_hw_constraint_list(substream->runtime, 0,
  153. SNDRV_PCM_HW_PARAM_RATE,
  154. wm8741->sysclk_constraints);
  155. return 0;
  156. }
  157. static int wm8741_hw_params(struct snd_pcm_substream *substream,
  158. struct snd_pcm_hw_params *params,
  159. struct snd_soc_dai *dai)
  160. {
  161. struct snd_soc_component *component = dai->component;
  162. struct wm8741_priv *wm8741 = snd_soc_component_get_drvdata(component);
  163. unsigned int iface, mode;
  164. int i;
  165. /* The set of sample rates that can be supported depends on the
  166. * MCLK supplied to the CODEC - enforce this.
  167. */
  168. if (!wm8741->sysclk) {
  169. dev_err(component->dev,
  170. "No MCLK configured, call set_sysclk() on init or in hw_params\n");
  171. return -EINVAL;
  172. }
  173. /* Find a supported LRCLK rate */
  174. for (i = 0; i < wm8741->sysclk_constraints->count; i++) {
  175. if (wm8741->sysclk_constraints->list[i] == params_rate(params))
  176. break;
  177. }
  178. if (i == wm8741->sysclk_constraints->count) {
  179. dev_err(component->dev, "LRCLK %d unsupported with MCLK %d\n",
  180. params_rate(params), wm8741->sysclk);
  181. return -EINVAL;
  182. }
  183. /* bit size */
  184. switch (params_width(params)) {
  185. case 16:
  186. iface = 0x0;
  187. break;
  188. case 20:
  189. iface = 0x1;
  190. break;
  191. case 24:
  192. iface = 0x2;
  193. break;
  194. case 32:
  195. iface = 0x3;
  196. break;
  197. default:
  198. dev_dbg(component->dev, "wm8741_hw_params: Unsupported bit size param = %d",
  199. params_width(params));
  200. return -EINVAL;
  201. }
  202. /* oversampling rate */
  203. if (params_rate(params) > 96000)
  204. mode = 0x40;
  205. else if (params_rate(params) > 48000)
  206. mode = 0x20;
  207. else
  208. mode = 0x00;
  209. dev_dbg(component->dev, "wm8741_hw_params: bit size param = %d, rate param = %d",
  210. params_width(params), params_rate(params));
  211. snd_soc_component_update_bits(component, WM8741_FORMAT_CONTROL, WM8741_IWL_MASK,
  212. iface);
  213. snd_soc_component_update_bits(component, WM8741_MODE_CONTROL_1, WM8741_OSR_MASK,
  214. mode);
  215. return 0;
  216. }
  217. static int wm8741_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  218. int clk_id, unsigned int freq, int dir)
  219. {
  220. struct snd_soc_component *component = codec_dai->component;
  221. struct wm8741_priv *wm8741 = snd_soc_component_get_drvdata(component);
  222. dev_dbg(component->dev, "wm8741_set_dai_sysclk info: freq=%dHz\n", freq);
  223. switch (freq) {
  224. case 0:
  225. wm8741->sysclk_constraints = NULL;
  226. break;
  227. case 11289600:
  228. wm8741->sysclk_constraints = &constraints_11289;
  229. break;
  230. case 12288000:
  231. wm8741->sysclk_constraints = &constraints_12288;
  232. break;
  233. case 16384000:
  234. wm8741->sysclk_constraints = &constraints_16384;
  235. break;
  236. case 16934400:
  237. wm8741->sysclk_constraints = &constraints_16934;
  238. break;
  239. case 18432000:
  240. wm8741->sysclk_constraints = &constraints_18432;
  241. break;
  242. case 22579200:
  243. case 33868800:
  244. wm8741->sysclk_constraints = &constraints_22579;
  245. break;
  246. case 24576000:
  247. wm8741->sysclk_constraints = &constraints_24576;
  248. break;
  249. case 36864000:
  250. wm8741->sysclk_constraints = &constraints_36864;
  251. break;
  252. default:
  253. return -EINVAL;
  254. }
  255. wm8741->sysclk = freq;
  256. return 0;
  257. }
  258. static int wm8741_set_dai_fmt(struct snd_soc_dai *codec_dai,
  259. unsigned int fmt)
  260. {
  261. struct snd_soc_component *component = codec_dai->component;
  262. unsigned int iface;
  263. /* check master/slave audio interface */
  264. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  265. case SND_SOC_DAIFMT_CBS_CFS:
  266. break;
  267. default:
  268. return -EINVAL;
  269. }
  270. /* interface format */
  271. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  272. case SND_SOC_DAIFMT_I2S:
  273. iface = 0x08;
  274. break;
  275. case SND_SOC_DAIFMT_RIGHT_J:
  276. iface = 0x00;
  277. break;
  278. case SND_SOC_DAIFMT_LEFT_J:
  279. iface = 0x04;
  280. break;
  281. case SND_SOC_DAIFMT_DSP_A:
  282. iface = 0x0C;
  283. break;
  284. case SND_SOC_DAIFMT_DSP_B:
  285. iface = 0x1C;
  286. break;
  287. default:
  288. return -EINVAL;
  289. }
  290. /* clock inversion */
  291. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  292. case SND_SOC_DAIFMT_NB_NF:
  293. break;
  294. case SND_SOC_DAIFMT_NB_IF:
  295. iface |= 0x10;
  296. break;
  297. case SND_SOC_DAIFMT_IB_NF:
  298. iface |= 0x20;
  299. break;
  300. case SND_SOC_DAIFMT_IB_IF:
  301. iface |= 0x30;
  302. break;
  303. default:
  304. return -EINVAL;
  305. }
  306. dev_dbg(component->dev, "wm8741_set_dai_fmt: Format=%x, Clock Inv=%x\n",
  307. fmt & SND_SOC_DAIFMT_FORMAT_MASK,
  308. ((fmt & SND_SOC_DAIFMT_INV_MASK)));
  309. snd_soc_component_update_bits(component, WM8741_FORMAT_CONTROL,
  310. WM8741_BCP_MASK | WM8741_LRP_MASK | WM8741_FMT_MASK,
  311. iface);
  312. return 0;
  313. }
  314. static int wm8741_mute(struct snd_soc_dai *codec_dai, int mute, int direction)
  315. {
  316. struct snd_soc_component *component = codec_dai->component;
  317. snd_soc_component_update_bits(component, WM8741_VOLUME_CONTROL,
  318. WM8741_SOFT_MASK, !!mute << WM8741_SOFT_SHIFT);
  319. return 0;
  320. }
  321. #define WM8741_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
  322. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | \
  323. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 | \
  324. SNDRV_PCM_RATE_192000)
  325. #define WM8741_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  326. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  327. static const struct snd_soc_dai_ops wm8741_dai_ops = {
  328. .startup = wm8741_startup,
  329. .hw_params = wm8741_hw_params,
  330. .set_sysclk = wm8741_set_dai_sysclk,
  331. .set_fmt = wm8741_set_dai_fmt,
  332. .mute_stream = wm8741_mute,
  333. .no_capture_mute = 1,
  334. };
  335. static struct snd_soc_dai_driver wm8741_dai = {
  336. .name = "wm8741",
  337. .playback = {
  338. .stream_name = "Playback",
  339. .channels_min = 2,
  340. .channels_max = 2,
  341. .rates = WM8741_RATES,
  342. .formats = WM8741_FORMATS,
  343. },
  344. .ops = &wm8741_dai_ops,
  345. };
  346. #ifdef CONFIG_PM
  347. static int wm8741_resume(struct snd_soc_component *component)
  348. {
  349. snd_soc_component_cache_sync(component);
  350. return 0;
  351. }
  352. #else
  353. #define wm8741_resume NULL
  354. #endif
  355. static int wm8741_configure(struct snd_soc_component *component)
  356. {
  357. struct wm8741_priv *wm8741 = snd_soc_component_get_drvdata(component);
  358. /* Configure differential mode */
  359. switch (wm8741->pdata.diff_mode) {
  360. case WM8741_DIFF_MODE_STEREO:
  361. case WM8741_DIFF_MODE_STEREO_REVERSED:
  362. case WM8741_DIFF_MODE_MONO_LEFT:
  363. case WM8741_DIFF_MODE_MONO_RIGHT:
  364. snd_soc_component_update_bits(component, WM8741_MODE_CONTROL_2,
  365. WM8741_DIFF_MASK,
  366. wm8741->pdata.diff_mode << WM8741_DIFF_SHIFT);
  367. break;
  368. default:
  369. return -EINVAL;
  370. }
  371. /* Change some default settings - latch VU */
  372. snd_soc_component_update_bits(component, WM8741_DACLLSB_ATTENUATION,
  373. WM8741_UPDATELL, WM8741_UPDATELL);
  374. snd_soc_component_update_bits(component, WM8741_DACLMSB_ATTENUATION,
  375. WM8741_UPDATELM, WM8741_UPDATELM);
  376. snd_soc_component_update_bits(component, WM8741_DACRLSB_ATTENUATION,
  377. WM8741_UPDATERL, WM8741_UPDATERL);
  378. snd_soc_component_update_bits(component, WM8741_DACRMSB_ATTENUATION,
  379. WM8741_UPDATERM, WM8741_UPDATERM);
  380. return 0;
  381. }
  382. static int wm8741_add_controls(struct snd_soc_component *component)
  383. {
  384. struct wm8741_priv *wm8741 = snd_soc_component_get_drvdata(component);
  385. switch (wm8741->pdata.diff_mode) {
  386. case WM8741_DIFF_MODE_STEREO:
  387. case WM8741_DIFF_MODE_STEREO_REVERSED:
  388. snd_soc_add_component_controls(component,
  389. wm8741_snd_controls_stereo,
  390. ARRAY_SIZE(wm8741_snd_controls_stereo));
  391. break;
  392. case WM8741_DIFF_MODE_MONO_LEFT:
  393. snd_soc_add_component_controls(component,
  394. wm8741_snd_controls_mono_left,
  395. ARRAY_SIZE(wm8741_snd_controls_mono_left));
  396. break;
  397. case WM8741_DIFF_MODE_MONO_RIGHT:
  398. snd_soc_add_component_controls(component,
  399. wm8741_snd_controls_mono_right,
  400. ARRAY_SIZE(wm8741_snd_controls_mono_right));
  401. break;
  402. default:
  403. return -EINVAL;
  404. }
  405. return 0;
  406. }
  407. static int wm8741_probe(struct snd_soc_component *component)
  408. {
  409. struct wm8741_priv *wm8741 = snd_soc_component_get_drvdata(component);
  410. int ret = 0;
  411. ret = regulator_bulk_enable(ARRAY_SIZE(wm8741->supplies),
  412. wm8741->supplies);
  413. if (ret != 0) {
  414. dev_err(component->dev, "Failed to enable supplies: %d\n", ret);
  415. goto err_get;
  416. }
  417. ret = wm8741_reset(component);
  418. if (ret < 0) {
  419. dev_err(component->dev, "Failed to issue reset\n");
  420. goto err_enable;
  421. }
  422. ret = wm8741_configure(component);
  423. if (ret < 0) {
  424. dev_err(component->dev, "Failed to change default settings\n");
  425. goto err_enable;
  426. }
  427. ret = wm8741_add_controls(component);
  428. if (ret < 0) {
  429. dev_err(component->dev, "Failed to add controls\n");
  430. goto err_enable;
  431. }
  432. dev_dbg(component->dev, "Successful registration\n");
  433. return ret;
  434. err_enable:
  435. regulator_bulk_disable(ARRAY_SIZE(wm8741->supplies), wm8741->supplies);
  436. err_get:
  437. return ret;
  438. }
  439. static void wm8741_remove(struct snd_soc_component *component)
  440. {
  441. struct wm8741_priv *wm8741 = snd_soc_component_get_drvdata(component);
  442. regulator_bulk_disable(ARRAY_SIZE(wm8741->supplies), wm8741->supplies);
  443. }
  444. static const struct snd_soc_component_driver soc_component_dev_wm8741 = {
  445. .probe = wm8741_probe,
  446. .remove = wm8741_remove,
  447. .resume = wm8741_resume,
  448. .dapm_widgets = wm8741_dapm_widgets,
  449. .num_dapm_widgets = ARRAY_SIZE(wm8741_dapm_widgets),
  450. .dapm_routes = wm8741_dapm_routes,
  451. .num_dapm_routes = ARRAY_SIZE(wm8741_dapm_routes),
  452. .idle_bias_on = 1,
  453. .use_pmdown_time = 1,
  454. .endianness = 1,
  455. };
  456. static const struct of_device_id wm8741_of_match[] = {
  457. { .compatible = "wlf,wm8741", },
  458. { }
  459. };
  460. MODULE_DEVICE_TABLE(of, wm8741_of_match);
  461. static const struct regmap_config wm8741_regmap = {
  462. .reg_bits = 7,
  463. .val_bits = 9,
  464. .max_register = WM8741_MAX_REGISTER,
  465. .reg_defaults = wm8741_reg_defaults,
  466. .num_reg_defaults = ARRAY_SIZE(wm8741_reg_defaults),
  467. .cache_type = REGCACHE_RBTREE,
  468. };
  469. static int wm8741_set_pdata(struct device *dev, struct wm8741_priv *wm8741)
  470. {
  471. const struct wm8741_platform_data *pdata = dev_get_platdata(dev);
  472. u32 diff_mode;
  473. if (dev->of_node) {
  474. if (of_property_read_u32(dev->of_node, "diff-mode", &diff_mode)
  475. >= 0)
  476. wm8741->pdata.diff_mode = diff_mode;
  477. } else {
  478. if (pdata != NULL)
  479. memcpy(&wm8741->pdata, pdata, sizeof(wm8741->pdata));
  480. }
  481. return 0;
  482. }
  483. #if IS_ENABLED(CONFIG_I2C)
  484. static int wm8741_i2c_probe(struct i2c_client *i2c)
  485. {
  486. struct wm8741_priv *wm8741;
  487. int ret, i;
  488. wm8741 = devm_kzalloc(&i2c->dev, sizeof(struct wm8741_priv),
  489. GFP_KERNEL);
  490. if (wm8741 == NULL)
  491. return -ENOMEM;
  492. for (i = 0; i < ARRAY_SIZE(wm8741->supplies); i++)
  493. wm8741->supplies[i].supply = wm8741_supply_names[i];
  494. ret = devm_regulator_bulk_get(&i2c->dev, ARRAY_SIZE(wm8741->supplies),
  495. wm8741->supplies);
  496. if (ret != 0) {
  497. dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret);
  498. return ret;
  499. }
  500. wm8741->regmap = devm_regmap_init_i2c(i2c, &wm8741_regmap);
  501. if (IS_ERR(wm8741->regmap)) {
  502. ret = PTR_ERR(wm8741->regmap);
  503. dev_err(&i2c->dev, "Failed to init regmap: %d\n", ret);
  504. return ret;
  505. }
  506. ret = wm8741_set_pdata(&i2c->dev, wm8741);
  507. if (ret != 0) {
  508. dev_err(&i2c->dev, "Failed to set pdata: %d\n", ret);
  509. return ret;
  510. }
  511. i2c_set_clientdata(i2c, wm8741);
  512. ret = devm_snd_soc_register_component(&i2c->dev,
  513. &soc_component_dev_wm8741, &wm8741_dai, 1);
  514. return ret;
  515. }
  516. static const struct i2c_device_id wm8741_i2c_id[] = {
  517. { "wm8741", 0 },
  518. { }
  519. };
  520. MODULE_DEVICE_TABLE(i2c, wm8741_i2c_id);
  521. static struct i2c_driver wm8741_i2c_driver = {
  522. .driver = {
  523. .name = "wm8741",
  524. .of_match_table = wm8741_of_match,
  525. },
  526. .probe_new = wm8741_i2c_probe,
  527. .id_table = wm8741_i2c_id,
  528. };
  529. #endif
  530. #if defined(CONFIG_SPI_MASTER)
  531. static int wm8741_spi_probe(struct spi_device *spi)
  532. {
  533. struct wm8741_priv *wm8741;
  534. int ret, i;
  535. wm8741 = devm_kzalloc(&spi->dev, sizeof(struct wm8741_priv),
  536. GFP_KERNEL);
  537. if (wm8741 == NULL)
  538. return -ENOMEM;
  539. for (i = 0; i < ARRAY_SIZE(wm8741->supplies); i++)
  540. wm8741->supplies[i].supply = wm8741_supply_names[i];
  541. ret = devm_regulator_bulk_get(&spi->dev, ARRAY_SIZE(wm8741->supplies),
  542. wm8741->supplies);
  543. if (ret != 0) {
  544. dev_err(&spi->dev, "Failed to request supplies: %d\n", ret);
  545. return ret;
  546. }
  547. wm8741->regmap = devm_regmap_init_spi(spi, &wm8741_regmap);
  548. if (IS_ERR(wm8741->regmap)) {
  549. ret = PTR_ERR(wm8741->regmap);
  550. dev_err(&spi->dev, "Failed to init regmap: %d\n", ret);
  551. return ret;
  552. }
  553. ret = wm8741_set_pdata(&spi->dev, wm8741);
  554. if (ret != 0) {
  555. dev_err(&spi->dev, "Failed to set pdata: %d\n", ret);
  556. return ret;
  557. }
  558. spi_set_drvdata(spi, wm8741);
  559. ret = devm_snd_soc_register_component(&spi->dev,
  560. &soc_component_dev_wm8741, &wm8741_dai, 1);
  561. return ret;
  562. }
  563. static struct spi_driver wm8741_spi_driver = {
  564. .driver = {
  565. .name = "wm8741",
  566. .of_match_table = wm8741_of_match,
  567. },
  568. .probe = wm8741_spi_probe,
  569. };
  570. #endif /* CONFIG_SPI_MASTER */
  571. static int __init wm8741_modinit(void)
  572. {
  573. int ret = 0;
  574. #if IS_ENABLED(CONFIG_I2C)
  575. ret = i2c_add_driver(&wm8741_i2c_driver);
  576. if (ret != 0)
  577. pr_err("Failed to register WM8741 I2C driver: %d\n", ret);
  578. #endif
  579. #if defined(CONFIG_SPI_MASTER)
  580. ret = spi_register_driver(&wm8741_spi_driver);
  581. if (ret != 0) {
  582. printk(KERN_ERR "Failed to register wm8741 SPI driver: %d\n",
  583. ret);
  584. }
  585. #endif
  586. return ret;
  587. }
  588. module_init(wm8741_modinit);
  589. static void __exit wm8741_exit(void)
  590. {
  591. #if defined(CONFIG_SPI_MASTER)
  592. spi_unregister_driver(&wm8741_spi_driver);
  593. #endif
  594. #if IS_ENABLED(CONFIG_I2C)
  595. i2c_del_driver(&wm8741_i2c_driver);
  596. #endif
  597. }
  598. module_exit(wm8741_exit);
  599. MODULE_DESCRIPTION("ASoC WM8741 driver");
  600. MODULE_AUTHOR("Ian Lartey <[email protected]>");
  601. MODULE_LICENSE("GPL");