arizona-core.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470
  1. /*
  2. * Arizona core driver
  3. *
  4. * Copyright 2012 Wolfson Microelectronics plc
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/delay.h>
  14. #include <linux/err.h>
  15. #include <linux/gpio/consumer.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/mfd/core.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/of_device.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/regmap.h>
  23. #include <linux/regulator/consumer.h>
  24. #include <linux/regulator/machine.h>
  25. #include <linux/slab.h>
  26. #include <linux/ktime.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/mfd/arizona/core.h>
  29. #include <linux/mfd/arizona/registers.h>
  30. #include "arizona.h"
  31. static const char * const wm5102_core_supplies[] = {
  32. "AVDD",
  33. "DBVDD1",
  34. };
  35. int arizona_clk32k_enable(struct arizona *arizona)
  36. {
  37. int ret = 0;
  38. mutex_lock(&arizona->clk_lock);
  39. arizona->clk32k_ref++;
  40. if (arizona->clk32k_ref == 1) {
  41. switch (arizona->pdata.clk32k_src) {
  42. case ARIZONA_32KZ_MCLK1:
  43. ret = pm_runtime_get_sync(arizona->dev);
  44. if (ret != 0)
  45. goto err_ref;
  46. ret = clk_prepare_enable(arizona->mclk[ARIZONA_MCLK1]);
  47. if (ret != 0) {
  48. pm_runtime_put_sync(arizona->dev);
  49. goto err_ref;
  50. }
  51. break;
  52. case ARIZONA_32KZ_MCLK2:
  53. ret = clk_prepare_enable(arizona->mclk[ARIZONA_MCLK2]);
  54. if (ret != 0)
  55. goto err_ref;
  56. break;
  57. }
  58. ret = regmap_update_bits(arizona->regmap, ARIZONA_CLOCK_32K_1,
  59. ARIZONA_CLK_32K_ENA,
  60. ARIZONA_CLK_32K_ENA);
  61. }
  62. err_ref:
  63. if (ret != 0)
  64. arizona->clk32k_ref--;
  65. mutex_unlock(&arizona->clk_lock);
  66. return ret;
  67. }
  68. EXPORT_SYMBOL_GPL(arizona_clk32k_enable);
  69. int arizona_clk32k_disable(struct arizona *arizona)
  70. {
  71. mutex_lock(&arizona->clk_lock);
  72. BUG_ON(arizona->clk32k_ref <= 0);
  73. arizona->clk32k_ref--;
  74. if (arizona->clk32k_ref == 0) {
  75. regmap_update_bits(arizona->regmap, ARIZONA_CLOCK_32K_1,
  76. ARIZONA_CLK_32K_ENA, 0);
  77. switch (arizona->pdata.clk32k_src) {
  78. case ARIZONA_32KZ_MCLK1:
  79. pm_runtime_put_sync(arizona->dev);
  80. clk_disable_unprepare(arizona->mclk[ARIZONA_MCLK1]);
  81. break;
  82. case ARIZONA_32KZ_MCLK2:
  83. clk_disable_unprepare(arizona->mclk[ARIZONA_MCLK2]);
  84. break;
  85. }
  86. }
  87. mutex_unlock(&arizona->clk_lock);
  88. return 0;
  89. }
  90. EXPORT_SYMBOL_GPL(arizona_clk32k_disable);
  91. static irqreturn_t arizona_clkgen_err(int irq, void *data)
  92. {
  93. struct arizona *arizona = data;
  94. dev_err(arizona->dev, "CLKGEN error\n");
  95. return IRQ_HANDLED;
  96. }
  97. static irqreturn_t arizona_underclocked(int irq, void *data)
  98. {
  99. struct arizona *arizona = data;
  100. unsigned int val;
  101. int ret;
  102. ret = regmap_read(arizona->regmap, ARIZONA_INTERRUPT_RAW_STATUS_8,
  103. &val);
  104. if (ret != 0) {
  105. dev_err(arizona->dev, "Failed to read underclock status: %d\n",
  106. ret);
  107. return IRQ_NONE;
  108. }
  109. if (val & ARIZONA_AIF3_UNDERCLOCKED_STS)
  110. dev_err(arizona->dev, "AIF3 underclocked\n");
  111. if (val & ARIZONA_AIF2_UNDERCLOCKED_STS)
  112. dev_err(arizona->dev, "AIF2 underclocked\n");
  113. if (val & ARIZONA_AIF1_UNDERCLOCKED_STS)
  114. dev_err(arizona->dev, "AIF1 underclocked\n");
  115. if (val & ARIZONA_ISRC3_UNDERCLOCKED_STS)
  116. dev_err(arizona->dev, "ISRC3 underclocked\n");
  117. if (val & ARIZONA_ISRC2_UNDERCLOCKED_STS)
  118. dev_err(arizona->dev, "ISRC2 underclocked\n");
  119. if (val & ARIZONA_ISRC1_UNDERCLOCKED_STS)
  120. dev_err(arizona->dev, "ISRC1 underclocked\n");
  121. if (val & ARIZONA_FX_UNDERCLOCKED_STS)
  122. dev_err(arizona->dev, "FX underclocked\n");
  123. if (val & ARIZONA_ASRC_UNDERCLOCKED_STS)
  124. dev_err(arizona->dev, "ASRC underclocked\n");
  125. if (val & ARIZONA_DAC_UNDERCLOCKED_STS)
  126. dev_err(arizona->dev, "DAC underclocked\n");
  127. if (val & ARIZONA_ADC_UNDERCLOCKED_STS)
  128. dev_err(arizona->dev, "ADC underclocked\n");
  129. if (val & ARIZONA_MIXER_UNDERCLOCKED_STS)
  130. dev_err(arizona->dev, "Mixer dropped sample\n");
  131. return IRQ_HANDLED;
  132. }
  133. static irqreturn_t arizona_overclocked(int irq, void *data)
  134. {
  135. struct arizona *arizona = data;
  136. unsigned int val[3];
  137. int ret;
  138. ret = regmap_bulk_read(arizona->regmap, ARIZONA_INTERRUPT_RAW_STATUS_6,
  139. &val[0], 3);
  140. if (ret != 0) {
  141. dev_err(arizona->dev, "Failed to read overclock status: %d\n",
  142. ret);
  143. return IRQ_NONE;
  144. }
  145. switch (arizona->type) {
  146. case WM8998:
  147. case WM1814:
  148. /* Some bits are shifted on WM8998,
  149. * rearrange to match the standard bit layout
  150. */
  151. val[0] = ((val[0] & 0x60e0) >> 1) |
  152. ((val[0] & 0x1e00) >> 2) |
  153. (val[0] & 0x000f);
  154. break;
  155. default:
  156. break;
  157. }
  158. if (val[0] & ARIZONA_PWM_OVERCLOCKED_STS)
  159. dev_err(arizona->dev, "PWM overclocked\n");
  160. if (val[0] & ARIZONA_FX_CORE_OVERCLOCKED_STS)
  161. dev_err(arizona->dev, "FX core overclocked\n");
  162. if (val[0] & ARIZONA_DAC_SYS_OVERCLOCKED_STS)
  163. dev_err(arizona->dev, "DAC SYS overclocked\n");
  164. if (val[0] & ARIZONA_DAC_WARP_OVERCLOCKED_STS)
  165. dev_err(arizona->dev, "DAC WARP overclocked\n");
  166. if (val[0] & ARIZONA_ADC_OVERCLOCKED_STS)
  167. dev_err(arizona->dev, "ADC overclocked\n");
  168. if (val[0] & ARIZONA_MIXER_OVERCLOCKED_STS)
  169. dev_err(arizona->dev, "Mixer overclocked\n");
  170. if (val[0] & ARIZONA_AIF3_SYNC_OVERCLOCKED_STS)
  171. dev_err(arizona->dev, "AIF3 overclocked\n");
  172. if (val[0] & ARIZONA_AIF2_SYNC_OVERCLOCKED_STS)
  173. dev_err(arizona->dev, "AIF2 overclocked\n");
  174. if (val[0] & ARIZONA_AIF1_SYNC_OVERCLOCKED_STS)
  175. dev_err(arizona->dev, "AIF1 overclocked\n");
  176. if (val[0] & ARIZONA_PAD_CTRL_OVERCLOCKED_STS)
  177. dev_err(arizona->dev, "Pad control overclocked\n");
  178. if (val[1] & ARIZONA_SLIMBUS_SUBSYS_OVERCLOCKED_STS)
  179. dev_err(arizona->dev, "Slimbus subsystem overclocked\n");
  180. if (val[1] & ARIZONA_SLIMBUS_ASYNC_OVERCLOCKED_STS)
  181. dev_err(arizona->dev, "Slimbus async overclocked\n");
  182. if (val[1] & ARIZONA_SLIMBUS_SYNC_OVERCLOCKED_STS)
  183. dev_err(arizona->dev, "Slimbus sync overclocked\n");
  184. if (val[1] & ARIZONA_ASRC_ASYNC_SYS_OVERCLOCKED_STS)
  185. dev_err(arizona->dev, "ASRC async system overclocked\n");
  186. if (val[1] & ARIZONA_ASRC_ASYNC_WARP_OVERCLOCKED_STS)
  187. dev_err(arizona->dev, "ASRC async WARP overclocked\n");
  188. if (val[1] & ARIZONA_ASRC_SYNC_SYS_OVERCLOCKED_STS)
  189. dev_err(arizona->dev, "ASRC sync system overclocked\n");
  190. if (val[1] & ARIZONA_ASRC_SYNC_WARP_OVERCLOCKED_STS)
  191. dev_err(arizona->dev, "ASRC sync WARP overclocked\n");
  192. if (val[1] & ARIZONA_ADSP2_1_OVERCLOCKED_STS)
  193. dev_err(arizona->dev, "DSP1 overclocked\n");
  194. if (val[1] & ARIZONA_ISRC3_OVERCLOCKED_STS)
  195. dev_err(arizona->dev, "ISRC3 overclocked\n");
  196. if (val[1] & ARIZONA_ISRC2_OVERCLOCKED_STS)
  197. dev_err(arizona->dev, "ISRC2 overclocked\n");
  198. if (val[1] & ARIZONA_ISRC1_OVERCLOCKED_STS)
  199. dev_err(arizona->dev, "ISRC1 overclocked\n");
  200. if (val[2] & ARIZONA_SPDIF_OVERCLOCKED_STS)
  201. dev_err(arizona->dev, "SPDIF overclocked\n");
  202. return IRQ_HANDLED;
  203. }
  204. #define ARIZONA_REG_POLL_DELAY_US 7500
  205. static inline bool arizona_poll_reg_delay(ktime_t timeout)
  206. {
  207. if (ktime_compare(ktime_get(), timeout) > 0)
  208. return false;
  209. usleep_range(ARIZONA_REG_POLL_DELAY_US / 2, ARIZONA_REG_POLL_DELAY_US);
  210. return true;
  211. }
  212. static int arizona_poll_reg(struct arizona *arizona,
  213. int timeout_ms, unsigned int reg,
  214. unsigned int mask, unsigned int target)
  215. {
  216. ktime_t timeout = ktime_add_us(ktime_get(), timeout_ms * USEC_PER_MSEC);
  217. unsigned int val = 0;
  218. int ret;
  219. do {
  220. ret = regmap_read(arizona->regmap, reg, &val);
  221. if ((val & mask) == target)
  222. return 0;
  223. } while (arizona_poll_reg_delay(timeout));
  224. if (ret) {
  225. dev_err(arizona->dev, "Failed polling reg 0x%x: %d\n",
  226. reg, ret);
  227. return ret;
  228. }
  229. dev_err(arizona->dev, "Polling reg 0x%x timed out: %x\n", reg, val);
  230. return -ETIMEDOUT;
  231. }
  232. static int arizona_wait_for_boot(struct arizona *arizona)
  233. {
  234. int ret;
  235. /*
  236. * We can't use an interrupt as we need to runtime resume to do so,
  237. * we won't race with the interrupt handler as it'll be blocked on
  238. * runtime resume.
  239. */
  240. ret = arizona_poll_reg(arizona, 30, ARIZONA_INTERRUPT_RAW_STATUS_5,
  241. ARIZONA_BOOT_DONE_STS, ARIZONA_BOOT_DONE_STS);
  242. if (!ret)
  243. regmap_write(arizona->regmap, ARIZONA_INTERRUPT_STATUS_5,
  244. ARIZONA_BOOT_DONE_STS);
  245. pm_runtime_mark_last_busy(arizona->dev);
  246. return ret;
  247. }
  248. static inline void arizona_enable_reset(struct arizona *arizona)
  249. {
  250. if (arizona->pdata.reset)
  251. gpiod_set_raw_value_cansleep(arizona->pdata.reset, 0);
  252. }
  253. static void arizona_disable_reset(struct arizona *arizona)
  254. {
  255. if (arizona->pdata.reset) {
  256. switch (arizona->type) {
  257. case WM5110:
  258. case WM8280:
  259. /* Meet requirements for minimum reset duration */
  260. usleep_range(5000, 10000);
  261. break;
  262. default:
  263. break;
  264. }
  265. gpiod_set_raw_value_cansleep(arizona->pdata.reset, 1);
  266. usleep_range(1000, 5000);
  267. }
  268. }
  269. struct arizona_sysclk_state {
  270. unsigned int fll;
  271. unsigned int sysclk;
  272. };
  273. static int arizona_enable_freerun_sysclk(struct arizona *arizona,
  274. struct arizona_sysclk_state *state)
  275. {
  276. int ret, err;
  277. /* Cache existing FLL and SYSCLK settings */
  278. ret = regmap_read(arizona->regmap, ARIZONA_FLL1_CONTROL_1, &state->fll);
  279. if (ret) {
  280. dev_err(arizona->dev, "Failed to cache FLL settings: %d\n",
  281. ret);
  282. return ret;
  283. }
  284. ret = regmap_read(arizona->regmap, ARIZONA_SYSTEM_CLOCK_1,
  285. &state->sysclk);
  286. if (ret) {
  287. dev_err(arizona->dev, "Failed to cache SYSCLK settings: %d\n",
  288. ret);
  289. return ret;
  290. }
  291. /* Start up SYSCLK using the FLL in free running mode */
  292. ret = regmap_write(arizona->regmap, ARIZONA_FLL1_CONTROL_1,
  293. ARIZONA_FLL1_ENA | ARIZONA_FLL1_FREERUN);
  294. if (ret) {
  295. dev_err(arizona->dev,
  296. "Failed to start FLL in freerunning mode: %d\n",
  297. ret);
  298. return ret;
  299. }
  300. ret = arizona_poll_reg(arizona, 180, ARIZONA_INTERRUPT_RAW_STATUS_5,
  301. ARIZONA_FLL1_CLOCK_OK_STS,
  302. ARIZONA_FLL1_CLOCK_OK_STS);
  303. if (ret)
  304. goto err_fll;
  305. ret = regmap_write(arizona->regmap, ARIZONA_SYSTEM_CLOCK_1, 0x0144);
  306. if (ret) {
  307. dev_err(arizona->dev, "Failed to start SYSCLK: %d\n", ret);
  308. goto err_fll;
  309. }
  310. return 0;
  311. err_fll:
  312. err = regmap_write(arizona->regmap, ARIZONA_FLL1_CONTROL_1, state->fll);
  313. if (err)
  314. dev_err(arizona->dev,
  315. "Failed to re-apply old FLL settings: %d\n", err);
  316. return ret;
  317. }
  318. static int arizona_disable_freerun_sysclk(struct arizona *arizona,
  319. struct arizona_sysclk_state *state)
  320. {
  321. int ret;
  322. ret = regmap_write(arizona->regmap, ARIZONA_SYSTEM_CLOCK_1,
  323. state->sysclk);
  324. if (ret) {
  325. dev_err(arizona->dev,
  326. "Failed to re-apply old SYSCLK settings: %d\n", ret);
  327. return ret;
  328. }
  329. ret = regmap_write(arizona->regmap, ARIZONA_FLL1_CONTROL_1, state->fll);
  330. if (ret) {
  331. dev_err(arizona->dev,
  332. "Failed to re-apply old FLL settings: %d\n", ret);
  333. return ret;
  334. }
  335. return 0;
  336. }
  337. static int wm5102_apply_hardware_patch(struct arizona *arizona)
  338. {
  339. struct arizona_sysclk_state state;
  340. int err, ret;
  341. ret = arizona_enable_freerun_sysclk(arizona, &state);
  342. if (ret)
  343. return ret;
  344. /* Start the write sequencer and wait for it to finish */
  345. ret = regmap_write(arizona->regmap, ARIZONA_WRITE_SEQUENCER_CTRL_0,
  346. ARIZONA_WSEQ_ENA | ARIZONA_WSEQ_START | 160);
  347. if (ret) {
  348. dev_err(arizona->dev, "Failed to start write sequencer: %d\n",
  349. ret);
  350. goto err;
  351. }
  352. ret = arizona_poll_reg(arizona, 30, ARIZONA_WRITE_SEQUENCER_CTRL_1,
  353. ARIZONA_WSEQ_BUSY, 0);
  354. if (ret)
  355. regmap_write(arizona->regmap, ARIZONA_WRITE_SEQUENCER_CTRL_0,
  356. ARIZONA_WSEQ_ABORT);
  357. err:
  358. err = arizona_disable_freerun_sysclk(arizona, &state);
  359. return ret ?: err;
  360. }
  361. /*
  362. * Register patch to some of the CODECs internal write sequences
  363. * to ensure a clean exit from the low power sleep state.
  364. */
  365. static const struct reg_sequence wm5110_sleep_patch[] = {
  366. { 0x337A, 0xC100 },
  367. { 0x337B, 0x0041 },
  368. { 0x3300, 0xA210 },
  369. { 0x3301, 0x050C },
  370. };
  371. static int wm5110_apply_sleep_patch(struct arizona *arizona)
  372. {
  373. struct arizona_sysclk_state state;
  374. int err, ret;
  375. ret = arizona_enable_freerun_sysclk(arizona, &state);
  376. if (ret)
  377. return ret;
  378. ret = regmap_multi_reg_write_bypassed(arizona->regmap,
  379. wm5110_sleep_patch,
  380. ARRAY_SIZE(wm5110_sleep_patch));
  381. err = arizona_disable_freerun_sysclk(arizona, &state);
  382. return ret ?: err;
  383. }
  384. static int wm5102_clear_write_sequencer(struct arizona *arizona)
  385. {
  386. int ret;
  387. ret = regmap_write(arizona->regmap, ARIZONA_WRITE_SEQUENCER_CTRL_3,
  388. 0x0);
  389. if (ret) {
  390. dev_err(arizona->dev,
  391. "Failed to clear write sequencer state: %d\n", ret);
  392. return ret;
  393. }
  394. arizona_enable_reset(arizona);
  395. regulator_disable(arizona->dcvdd);
  396. msleep(20);
  397. ret = regulator_enable(arizona->dcvdd);
  398. if (ret) {
  399. dev_err(arizona->dev, "Failed to re-enable DCVDD: %d\n", ret);
  400. return ret;
  401. }
  402. arizona_disable_reset(arizona);
  403. return 0;
  404. }
  405. #ifdef CONFIG_PM
  406. static int arizona_isolate_dcvdd(struct arizona *arizona)
  407. {
  408. int ret;
  409. ret = regmap_update_bits(arizona->regmap,
  410. ARIZONA_ISOLATION_CONTROL,
  411. ARIZONA_ISOLATE_DCVDD1,
  412. ARIZONA_ISOLATE_DCVDD1);
  413. if (ret != 0)
  414. dev_err(arizona->dev, "Failed to isolate DCVDD: %d\n", ret);
  415. return ret;
  416. }
  417. static int arizona_connect_dcvdd(struct arizona *arizona)
  418. {
  419. int ret;
  420. ret = regmap_update_bits(arizona->regmap,
  421. ARIZONA_ISOLATION_CONTROL,
  422. ARIZONA_ISOLATE_DCVDD1, 0);
  423. if (ret != 0)
  424. dev_err(arizona->dev, "Failed to connect DCVDD: %d\n", ret);
  425. return ret;
  426. }
  427. static int arizona_is_jack_det_active(struct arizona *arizona)
  428. {
  429. unsigned int val;
  430. int ret;
  431. ret = regmap_read(arizona->regmap, ARIZONA_JACK_DETECT_ANALOGUE, &val);
  432. if (ret) {
  433. dev_err(arizona->dev,
  434. "Failed to check jack det status: %d\n", ret);
  435. return ret;
  436. } else if (val & ARIZONA_JD1_ENA) {
  437. return 1;
  438. } else {
  439. return 0;
  440. }
  441. }
  442. static int arizona_runtime_resume(struct device *dev)
  443. {
  444. struct arizona *arizona = dev_get_drvdata(dev);
  445. int ret;
  446. dev_dbg(arizona->dev, "Leaving AoD mode\n");
  447. if (arizona->has_fully_powered_off) {
  448. dev_dbg(arizona->dev, "Re-enabling core supplies\n");
  449. ret = regulator_bulk_enable(arizona->num_core_supplies,
  450. arizona->core_supplies);
  451. if (ret) {
  452. dev_err(dev, "Failed to enable core supplies: %d\n",
  453. ret);
  454. return ret;
  455. }
  456. }
  457. ret = regulator_enable(arizona->dcvdd);
  458. if (ret != 0) {
  459. dev_err(arizona->dev, "Failed to enable DCVDD: %d\n", ret);
  460. if (arizona->has_fully_powered_off)
  461. regulator_bulk_disable(arizona->num_core_supplies,
  462. arizona->core_supplies);
  463. return ret;
  464. }
  465. if (arizona->has_fully_powered_off) {
  466. arizona_disable_reset(arizona);
  467. enable_irq(arizona->irq);
  468. arizona->has_fully_powered_off = false;
  469. }
  470. regcache_cache_only(arizona->regmap, false);
  471. switch (arizona->type) {
  472. case WM5102:
  473. if (arizona->external_dcvdd) {
  474. ret = arizona_connect_dcvdd(arizona);
  475. if (ret != 0)
  476. goto err;
  477. }
  478. ret = wm5102_patch(arizona);
  479. if (ret != 0) {
  480. dev_err(arizona->dev, "Failed to apply patch: %d\n",
  481. ret);
  482. goto err;
  483. }
  484. ret = wm5102_apply_hardware_patch(arizona);
  485. if (ret) {
  486. dev_err(arizona->dev,
  487. "Failed to apply hardware patch: %d\n",
  488. ret);
  489. goto err;
  490. }
  491. break;
  492. case WM5110:
  493. case WM8280:
  494. ret = arizona_wait_for_boot(arizona);
  495. if (ret)
  496. goto err;
  497. if (arizona->external_dcvdd) {
  498. ret = arizona_connect_dcvdd(arizona);
  499. if (ret != 0)
  500. goto err;
  501. } else {
  502. /*
  503. * As this is only called for the internal regulator
  504. * (where we know voltage ranges available) it is ok
  505. * to request an exact range.
  506. */
  507. ret = regulator_set_voltage(arizona->dcvdd,
  508. 1200000, 1200000);
  509. if (ret < 0) {
  510. dev_err(arizona->dev,
  511. "Failed to set resume voltage: %d\n",
  512. ret);
  513. goto err;
  514. }
  515. }
  516. ret = wm5110_apply_sleep_patch(arizona);
  517. if (ret) {
  518. dev_err(arizona->dev,
  519. "Failed to re-apply sleep patch: %d\n",
  520. ret);
  521. goto err;
  522. }
  523. break;
  524. case WM1831:
  525. case CS47L24:
  526. ret = arizona_wait_for_boot(arizona);
  527. if (ret != 0)
  528. goto err;
  529. break;
  530. default:
  531. ret = arizona_wait_for_boot(arizona);
  532. if (ret != 0)
  533. goto err;
  534. if (arizona->external_dcvdd) {
  535. ret = arizona_connect_dcvdd(arizona);
  536. if (ret != 0)
  537. goto err;
  538. }
  539. break;
  540. }
  541. ret = regcache_sync(arizona->regmap);
  542. if (ret != 0) {
  543. dev_err(arizona->dev, "Failed to restore register cache\n");
  544. goto err;
  545. }
  546. return 0;
  547. err:
  548. regcache_cache_only(arizona->regmap, true);
  549. regulator_disable(arizona->dcvdd);
  550. return ret;
  551. }
  552. static int arizona_runtime_suspend(struct device *dev)
  553. {
  554. struct arizona *arizona = dev_get_drvdata(dev);
  555. int jd_active = 0;
  556. int ret;
  557. dev_dbg(arizona->dev, "Entering AoD mode\n");
  558. switch (arizona->type) {
  559. case WM5110:
  560. case WM8280:
  561. jd_active = arizona_is_jack_det_active(arizona);
  562. if (jd_active < 0)
  563. return jd_active;
  564. if (arizona->external_dcvdd) {
  565. ret = arizona_isolate_dcvdd(arizona);
  566. if (ret != 0)
  567. return ret;
  568. } else {
  569. /*
  570. * As this is only called for the internal regulator
  571. * (where we know voltage ranges available) it is ok
  572. * to request an exact range.
  573. */
  574. ret = regulator_set_voltage(arizona->dcvdd,
  575. 1175000, 1175000);
  576. if (ret < 0) {
  577. dev_err(arizona->dev,
  578. "Failed to set suspend voltage: %d\n",
  579. ret);
  580. return ret;
  581. }
  582. }
  583. break;
  584. case WM5102:
  585. jd_active = arizona_is_jack_det_active(arizona);
  586. if (jd_active < 0)
  587. return jd_active;
  588. if (arizona->external_dcvdd) {
  589. ret = arizona_isolate_dcvdd(arizona);
  590. if (ret != 0)
  591. return ret;
  592. }
  593. if (!jd_active) {
  594. ret = regmap_write(arizona->regmap,
  595. ARIZONA_WRITE_SEQUENCER_CTRL_3, 0x0);
  596. if (ret) {
  597. dev_err(arizona->dev,
  598. "Failed to clear write sequencer: %d\n",
  599. ret);
  600. return ret;
  601. }
  602. }
  603. break;
  604. case WM1831:
  605. case CS47L24:
  606. break;
  607. default:
  608. jd_active = arizona_is_jack_det_active(arizona);
  609. if (jd_active < 0)
  610. return jd_active;
  611. if (arizona->external_dcvdd) {
  612. ret = arizona_isolate_dcvdd(arizona);
  613. if (ret != 0)
  614. return ret;
  615. }
  616. break;
  617. }
  618. regcache_cache_only(arizona->regmap, true);
  619. regcache_mark_dirty(arizona->regmap);
  620. regulator_disable(arizona->dcvdd);
  621. /* Allow us to completely power down if no jack detection */
  622. if (!jd_active) {
  623. dev_dbg(arizona->dev, "Fully powering off\n");
  624. arizona->has_fully_powered_off = true;
  625. disable_irq_nosync(arizona->irq);
  626. arizona_enable_reset(arizona);
  627. regulator_bulk_disable(arizona->num_core_supplies,
  628. arizona->core_supplies);
  629. }
  630. return 0;
  631. }
  632. #endif
  633. #ifdef CONFIG_PM_SLEEP
  634. static int arizona_suspend(struct device *dev)
  635. {
  636. struct arizona *arizona = dev_get_drvdata(dev);
  637. dev_dbg(arizona->dev, "Suspend, disabling IRQ\n");
  638. disable_irq(arizona->irq);
  639. return 0;
  640. }
  641. static int arizona_suspend_noirq(struct device *dev)
  642. {
  643. struct arizona *arizona = dev_get_drvdata(dev);
  644. dev_dbg(arizona->dev, "Late suspend, reenabling IRQ\n");
  645. enable_irq(arizona->irq);
  646. return 0;
  647. }
  648. static int arizona_resume_noirq(struct device *dev)
  649. {
  650. struct arizona *arizona = dev_get_drvdata(dev);
  651. dev_dbg(arizona->dev, "Early resume, disabling IRQ\n");
  652. disable_irq(arizona->irq);
  653. return 0;
  654. }
  655. static int arizona_resume(struct device *dev)
  656. {
  657. struct arizona *arizona = dev_get_drvdata(dev);
  658. dev_dbg(arizona->dev, "Resume, reenabling IRQ\n");
  659. enable_irq(arizona->irq);
  660. return 0;
  661. }
  662. #endif
  663. const struct dev_pm_ops arizona_pm_ops = {
  664. SET_RUNTIME_PM_OPS(arizona_runtime_suspend,
  665. arizona_runtime_resume,
  666. NULL)
  667. SET_SYSTEM_SLEEP_PM_OPS(arizona_suspend, arizona_resume)
  668. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(arizona_suspend_noirq,
  669. arizona_resume_noirq)
  670. };
  671. EXPORT_SYMBOL_GPL(arizona_pm_ops);
  672. #ifdef CONFIG_OF
  673. unsigned long arizona_of_get_type(struct device *dev)
  674. {
  675. const struct of_device_id *id = of_match_device(arizona_of_match, dev);
  676. if (id)
  677. return (unsigned long)id->data;
  678. else
  679. return 0;
  680. }
  681. EXPORT_SYMBOL_GPL(arizona_of_get_type);
  682. static int arizona_of_get_core_pdata(struct arizona *arizona)
  683. {
  684. struct arizona_pdata *pdata = &arizona->pdata;
  685. int ret, i;
  686. /* Handle old non-standard DT binding */
  687. pdata->reset = devm_gpiod_get_from_of_node(arizona->dev,
  688. arizona->dev->of_node,
  689. "wlf,reset", 0,
  690. GPIOD_OUT_LOW,
  691. "arizona /RESET");
  692. if (IS_ERR(pdata->reset)) {
  693. ret = PTR_ERR(pdata->reset);
  694. /*
  695. * Reset missing will be caught when other binding is read
  696. * but all other errors imply this binding is in use but has
  697. * encountered a problem so should be handled.
  698. */
  699. if (ret == -EPROBE_DEFER)
  700. return ret;
  701. else if (ret != -ENOENT && ret != -ENOSYS)
  702. dev_err(arizona->dev, "Reset GPIO malformed: %d\n",
  703. ret);
  704. pdata->reset = NULL;
  705. }
  706. ret = of_property_read_u32_array(arizona->dev->of_node,
  707. "wlf,gpio-defaults",
  708. pdata->gpio_defaults,
  709. ARRAY_SIZE(pdata->gpio_defaults));
  710. if (ret >= 0) {
  711. /*
  712. * All values are literal except out of range values
  713. * which are chip default, translate into platform
  714. * data which uses 0 as chip default and out of range
  715. * as zero.
  716. */
  717. for (i = 0; i < ARRAY_SIZE(pdata->gpio_defaults); i++) {
  718. if (pdata->gpio_defaults[i] > 0xffff)
  719. pdata->gpio_defaults[i] = 0;
  720. else if (pdata->gpio_defaults[i] == 0)
  721. pdata->gpio_defaults[i] = 0x10000;
  722. }
  723. } else {
  724. dev_err(arizona->dev, "Failed to parse GPIO defaults: %d\n",
  725. ret);
  726. }
  727. return 0;
  728. }
  729. const struct of_device_id arizona_of_match[] = {
  730. { .compatible = "wlf,wm5102", .data = (void *)WM5102 },
  731. { .compatible = "wlf,wm5110", .data = (void *)WM5110 },
  732. { .compatible = "wlf,wm8280", .data = (void *)WM8280 },
  733. { .compatible = "wlf,wm8997", .data = (void *)WM8997 },
  734. { .compatible = "wlf,wm8998", .data = (void *)WM8998 },
  735. { .compatible = "wlf,wm1814", .data = (void *)WM1814 },
  736. { .compatible = "wlf,wm1831", .data = (void *)WM1831 },
  737. { .compatible = "cirrus,cs47l24", .data = (void *)CS47L24 },
  738. {},
  739. };
  740. EXPORT_SYMBOL_GPL(arizona_of_match);
  741. #else
  742. static inline int arizona_of_get_core_pdata(struct arizona *arizona)
  743. {
  744. return 0;
  745. }
  746. #endif
  747. static const struct mfd_cell early_devs[] = {
  748. { .name = "arizona-ldo1" },
  749. };
  750. static const char * const wm5102_supplies[] = {
  751. "MICVDD",
  752. "DBVDD2",
  753. "DBVDD3",
  754. "CPVDD",
  755. "SPKVDDL",
  756. "SPKVDDR",
  757. };
  758. static const struct mfd_cell wm5102_devs[] = {
  759. { .name = "arizona-micsupp" },
  760. { .name = "arizona-gpio" },
  761. {
  762. .name = "arizona-extcon",
  763. .parent_supplies = wm5102_supplies,
  764. .num_parent_supplies = 1, /* We only need MICVDD */
  765. },
  766. { .name = "arizona-haptics" },
  767. { .name = "arizona-pwm" },
  768. {
  769. .name = "wm5102-codec",
  770. .parent_supplies = wm5102_supplies,
  771. .num_parent_supplies = ARRAY_SIZE(wm5102_supplies),
  772. },
  773. };
  774. static const struct mfd_cell wm5110_devs[] = {
  775. { .name = "arizona-micsupp" },
  776. { .name = "arizona-gpio" },
  777. {
  778. .name = "arizona-extcon",
  779. .parent_supplies = wm5102_supplies,
  780. .num_parent_supplies = 1, /* We only need MICVDD */
  781. },
  782. { .name = "arizona-haptics" },
  783. { .name = "arizona-pwm" },
  784. {
  785. .name = "wm5110-codec",
  786. .parent_supplies = wm5102_supplies,
  787. .num_parent_supplies = ARRAY_SIZE(wm5102_supplies),
  788. },
  789. };
  790. static const char * const cs47l24_supplies[] = {
  791. "MICVDD",
  792. "CPVDD",
  793. "SPKVDD",
  794. };
  795. static const struct mfd_cell cs47l24_devs[] = {
  796. { .name = "arizona-gpio" },
  797. { .name = "arizona-haptics" },
  798. { .name = "arizona-pwm" },
  799. {
  800. .name = "cs47l24-codec",
  801. .parent_supplies = cs47l24_supplies,
  802. .num_parent_supplies = ARRAY_SIZE(cs47l24_supplies),
  803. },
  804. };
  805. static const char * const wm8997_supplies[] = {
  806. "MICVDD",
  807. "DBVDD2",
  808. "CPVDD",
  809. "SPKVDD",
  810. };
  811. static const struct mfd_cell wm8997_devs[] = {
  812. { .name = "arizona-micsupp" },
  813. { .name = "arizona-gpio" },
  814. {
  815. .name = "arizona-extcon",
  816. .parent_supplies = wm8997_supplies,
  817. .num_parent_supplies = 1, /* We only need MICVDD */
  818. },
  819. { .name = "arizona-haptics" },
  820. { .name = "arizona-pwm" },
  821. {
  822. .name = "wm8997-codec",
  823. .parent_supplies = wm8997_supplies,
  824. .num_parent_supplies = ARRAY_SIZE(wm8997_supplies),
  825. },
  826. };
  827. static const struct mfd_cell wm8998_devs[] = {
  828. { .name = "arizona-micsupp" },
  829. { .name = "arizona-gpio" },
  830. {
  831. .name = "arizona-extcon",
  832. .parent_supplies = wm5102_supplies,
  833. .num_parent_supplies = 1, /* We only need MICVDD */
  834. },
  835. { .name = "arizona-haptics" },
  836. { .name = "arizona-pwm" },
  837. {
  838. .name = "wm8998-codec",
  839. .parent_supplies = wm5102_supplies,
  840. .num_parent_supplies = ARRAY_SIZE(wm5102_supplies),
  841. },
  842. };
  843. int arizona_dev_init(struct arizona *arizona)
  844. {
  845. const char * const mclk_name[] = { "mclk1", "mclk2" };
  846. struct device *dev = arizona->dev;
  847. const char *type_name = NULL;
  848. unsigned int reg, val;
  849. int (*apply_patch)(struct arizona *) = NULL;
  850. const struct mfd_cell *subdevs = NULL;
  851. int n_subdevs = 0, ret, i;
  852. dev_set_drvdata(arizona->dev, arizona);
  853. mutex_init(&arizona->clk_lock);
  854. if (dev_get_platdata(arizona->dev)) {
  855. memcpy(&arizona->pdata, dev_get_platdata(arizona->dev),
  856. sizeof(arizona->pdata));
  857. } else {
  858. ret = arizona_of_get_core_pdata(arizona);
  859. if (ret < 0)
  860. return ret;
  861. }
  862. BUILD_BUG_ON(ARRAY_SIZE(arizona->mclk) != ARRAY_SIZE(mclk_name));
  863. for (i = 0; i < ARRAY_SIZE(arizona->mclk); i++) {
  864. arizona->mclk[i] = devm_clk_get(arizona->dev, mclk_name[i]);
  865. if (IS_ERR(arizona->mclk[i])) {
  866. dev_info(arizona->dev, "Failed to get %s: %ld\n",
  867. mclk_name[i], PTR_ERR(arizona->mclk[i]));
  868. arizona->mclk[i] = NULL;
  869. }
  870. }
  871. regcache_cache_only(arizona->regmap, true);
  872. switch (arizona->type) {
  873. case WM5102:
  874. case WM5110:
  875. case WM8280:
  876. case WM8997:
  877. case WM8998:
  878. case WM1814:
  879. case WM1831:
  880. case CS47L24:
  881. for (i = 0; i < ARRAY_SIZE(wm5102_core_supplies); i++)
  882. arizona->core_supplies[i].supply
  883. = wm5102_core_supplies[i];
  884. arizona->num_core_supplies = ARRAY_SIZE(wm5102_core_supplies);
  885. break;
  886. default:
  887. dev_err(arizona->dev, "Unknown device type %d\n",
  888. arizona->type);
  889. return -ENODEV;
  890. }
  891. /* Mark DCVDD as external, LDO1 driver will clear if internal */
  892. arizona->external_dcvdd = true;
  893. switch (arizona->type) {
  894. case WM1831:
  895. case CS47L24:
  896. break; /* No LDO1 regulator */
  897. default:
  898. ret = mfd_add_devices(arizona->dev, -1, early_devs,
  899. ARRAY_SIZE(early_devs), NULL, 0, NULL);
  900. if (ret != 0) {
  901. dev_err(dev, "Failed to add early children: %d\n", ret);
  902. return ret;
  903. }
  904. break;
  905. }
  906. ret = devm_regulator_bulk_get(dev, arizona->num_core_supplies,
  907. arizona->core_supplies);
  908. if (ret != 0) {
  909. dev_err(dev, "Failed to request core supplies: %d\n",
  910. ret);
  911. goto err_early;
  912. }
  913. /**
  914. * Don't use devres here because the only device we have to get
  915. * against is the MFD device and DCVDD will likely be supplied by
  916. * one of its children. Meaning that the regulator will be
  917. * destroyed by the time devres calls regulator put.
  918. */
  919. arizona->dcvdd = regulator_get(arizona->dev, "DCVDD");
  920. if (IS_ERR(arizona->dcvdd)) {
  921. ret = PTR_ERR(arizona->dcvdd);
  922. dev_err(dev, "Failed to request DCVDD: %d\n", ret);
  923. goto err_early;
  924. }
  925. if (!arizona->pdata.reset) {
  926. /* Start out with /RESET low to put the chip into reset */
  927. arizona->pdata.reset = devm_gpiod_get(arizona->dev, "reset",
  928. GPIOD_OUT_LOW);
  929. if (IS_ERR(arizona->pdata.reset)) {
  930. ret = PTR_ERR(arizona->pdata.reset);
  931. if (ret == -EPROBE_DEFER)
  932. goto err_dcvdd;
  933. dev_err(arizona->dev,
  934. "Reset GPIO missing/malformed: %d\n", ret);
  935. arizona->pdata.reset = NULL;
  936. }
  937. }
  938. ret = regulator_bulk_enable(arizona->num_core_supplies,
  939. arizona->core_supplies);
  940. if (ret != 0) {
  941. dev_err(dev, "Failed to enable core supplies: %d\n",
  942. ret);
  943. goto err_dcvdd;
  944. }
  945. ret = regulator_enable(arizona->dcvdd);
  946. if (ret != 0) {
  947. dev_err(dev, "Failed to enable DCVDD: %d\n", ret);
  948. goto err_enable;
  949. }
  950. arizona_disable_reset(arizona);
  951. regcache_cache_only(arizona->regmap, false);
  952. /* Verify that this is a chip we know about */
  953. ret = regmap_read(arizona->regmap, ARIZONA_SOFTWARE_RESET, &reg);
  954. if (ret != 0) {
  955. dev_err(dev, "Failed to read ID register: %d\n", ret);
  956. goto err_reset;
  957. }
  958. switch (reg) {
  959. case 0x5102:
  960. case 0x5110:
  961. case 0x6349:
  962. case 0x6363:
  963. case 0x8997:
  964. break;
  965. default:
  966. dev_err(arizona->dev, "Unknown device ID: %x\n", reg);
  967. ret = -ENODEV;
  968. goto err_reset;
  969. }
  970. /* If we have a /RESET GPIO we'll already be reset */
  971. if (!arizona->pdata.reset) {
  972. ret = regmap_write(arizona->regmap, ARIZONA_SOFTWARE_RESET, 0);
  973. if (ret != 0) {
  974. dev_err(dev, "Failed to reset device: %d\n", ret);
  975. goto err_reset;
  976. }
  977. usleep_range(1000, 5000);
  978. }
  979. /* Ensure device startup is complete */
  980. switch (arizona->type) {
  981. case WM5102:
  982. ret = regmap_read(arizona->regmap,
  983. ARIZONA_WRITE_SEQUENCER_CTRL_3, &val);
  984. if (ret) {
  985. dev_err(dev,
  986. "Failed to check write sequencer state: %d\n",
  987. ret);
  988. } else if (val & 0x01) {
  989. ret = wm5102_clear_write_sequencer(arizona);
  990. if (ret)
  991. return ret;
  992. }
  993. break;
  994. default:
  995. break;
  996. }
  997. ret = arizona_wait_for_boot(arizona);
  998. if (ret) {
  999. dev_err(arizona->dev, "Device failed initial boot: %d\n", ret);
  1000. goto err_reset;
  1001. }
  1002. /* Read the device ID information & do device specific stuff */
  1003. ret = regmap_read(arizona->regmap, ARIZONA_SOFTWARE_RESET, &reg);
  1004. if (ret != 0) {
  1005. dev_err(dev, "Failed to read ID register: %d\n", ret);
  1006. goto err_reset;
  1007. }
  1008. ret = regmap_read(arizona->regmap, ARIZONA_DEVICE_REVISION,
  1009. &arizona->rev);
  1010. if (ret != 0) {
  1011. dev_err(dev, "Failed to read revision register: %d\n", ret);
  1012. goto err_reset;
  1013. }
  1014. arizona->rev &= ARIZONA_DEVICE_REVISION_MASK;
  1015. switch (reg) {
  1016. case 0x5102:
  1017. if (IS_ENABLED(CONFIG_MFD_WM5102)) {
  1018. type_name = "WM5102";
  1019. if (arizona->type != WM5102) {
  1020. dev_warn(arizona->dev,
  1021. "WM5102 registered as %d\n",
  1022. arizona->type);
  1023. arizona->type = WM5102;
  1024. }
  1025. apply_patch = wm5102_patch;
  1026. arizona->rev &= 0x7;
  1027. subdevs = wm5102_devs;
  1028. n_subdevs = ARRAY_SIZE(wm5102_devs);
  1029. }
  1030. break;
  1031. case 0x5110:
  1032. if (IS_ENABLED(CONFIG_MFD_WM5110)) {
  1033. switch (arizona->type) {
  1034. case WM5110:
  1035. type_name = "WM5110";
  1036. break;
  1037. case WM8280:
  1038. type_name = "WM8280";
  1039. break;
  1040. default:
  1041. type_name = "WM5110";
  1042. dev_warn(arizona->dev,
  1043. "WM5110 registered as %d\n",
  1044. arizona->type);
  1045. arizona->type = WM5110;
  1046. break;
  1047. }
  1048. apply_patch = wm5110_patch;
  1049. subdevs = wm5110_devs;
  1050. n_subdevs = ARRAY_SIZE(wm5110_devs);
  1051. }
  1052. break;
  1053. case 0x6363:
  1054. if (IS_ENABLED(CONFIG_MFD_CS47L24)) {
  1055. switch (arizona->type) {
  1056. case CS47L24:
  1057. type_name = "CS47L24";
  1058. break;
  1059. case WM1831:
  1060. type_name = "WM1831";
  1061. break;
  1062. default:
  1063. dev_warn(arizona->dev,
  1064. "CS47L24 registered as %d\n",
  1065. arizona->type);
  1066. arizona->type = CS47L24;
  1067. break;
  1068. }
  1069. apply_patch = cs47l24_patch;
  1070. subdevs = cs47l24_devs;
  1071. n_subdevs = ARRAY_SIZE(cs47l24_devs);
  1072. }
  1073. break;
  1074. case 0x8997:
  1075. if (IS_ENABLED(CONFIG_MFD_WM8997)) {
  1076. type_name = "WM8997";
  1077. if (arizona->type != WM8997) {
  1078. dev_warn(arizona->dev,
  1079. "WM8997 registered as %d\n",
  1080. arizona->type);
  1081. arizona->type = WM8997;
  1082. }
  1083. apply_patch = wm8997_patch;
  1084. subdevs = wm8997_devs;
  1085. n_subdevs = ARRAY_SIZE(wm8997_devs);
  1086. }
  1087. break;
  1088. case 0x6349:
  1089. if (IS_ENABLED(CONFIG_MFD_WM8998)) {
  1090. switch (arizona->type) {
  1091. case WM8998:
  1092. type_name = "WM8998";
  1093. break;
  1094. case WM1814:
  1095. type_name = "WM1814";
  1096. break;
  1097. default:
  1098. type_name = "WM8998";
  1099. dev_warn(arizona->dev,
  1100. "WM8998 registered as %d\n",
  1101. arizona->type);
  1102. arizona->type = WM8998;
  1103. }
  1104. apply_patch = wm8998_patch;
  1105. subdevs = wm8998_devs;
  1106. n_subdevs = ARRAY_SIZE(wm8998_devs);
  1107. }
  1108. break;
  1109. default:
  1110. dev_err(arizona->dev, "Unknown device ID %x\n", reg);
  1111. ret = -ENODEV;
  1112. goto err_reset;
  1113. }
  1114. if (!subdevs) {
  1115. dev_err(arizona->dev,
  1116. "No kernel support for device ID %x\n", reg);
  1117. ret = -ENODEV;
  1118. goto err_reset;
  1119. }
  1120. dev_info(dev, "%s revision %c\n", type_name, arizona->rev + 'A');
  1121. if (apply_patch) {
  1122. ret = apply_patch(arizona);
  1123. if (ret != 0) {
  1124. dev_err(arizona->dev, "Failed to apply patch: %d\n",
  1125. ret);
  1126. goto err_reset;
  1127. }
  1128. switch (arizona->type) {
  1129. case WM5102:
  1130. ret = wm5102_apply_hardware_patch(arizona);
  1131. if (ret) {
  1132. dev_err(arizona->dev,
  1133. "Failed to apply hardware patch: %d\n",
  1134. ret);
  1135. goto err_reset;
  1136. }
  1137. break;
  1138. case WM5110:
  1139. case WM8280:
  1140. ret = wm5110_apply_sleep_patch(arizona);
  1141. if (ret) {
  1142. dev_err(arizona->dev,
  1143. "Failed to apply sleep patch: %d\n",
  1144. ret);
  1145. goto err_reset;
  1146. }
  1147. break;
  1148. default:
  1149. break;
  1150. }
  1151. }
  1152. for (i = 0; i < ARRAY_SIZE(arizona->pdata.gpio_defaults); i++) {
  1153. if (!arizona->pdata.gpio_defaults[i])
  1154. continue;
  1155. regmap_write(arizona->regmap, ARIZONA_GPIO1_CTRL + i,
  1156. arizona->pdata.gpio_defaults[i]);
  1157. }
  1158. /* Chip default */
  1159. if (!arizona->pdata.clk32k_src)
  1160. arizona->pdata.clk32k_src = ARIZONA_32KZ_MCLK2;
  1161. switch (arizona->pdata.clk32k_src) {
  1162. case ARIZONA_32KZ_MCLK1:
  1163. case ARIZONA_32KZ_MCLK2:
  1164. regmap_update_bits(arizona->regmap, ARIZONA_CLOCK_32K_1,
  1165. ARIZONA_CLK_32K_SRC_MASK,
  1166. arizona->pdata.clk32k_src - 1);
  1167. arizona_clk32k_enable(arizona);
  1168. break;
  1169. case ARIZONA_32KZ_NONE:
  1170. regmap_update_bits(arizona->regmap, ARIZONA_CLOCK_32K_1,
  1171. ARIZONA_CLK_32K_SRC_MASK, 2);
  1172. break;
  1173. default:
  1174. dev_err(arizona->dev, "Invalid 32kHz clock source: %d\n",
  1175. arizona->pdata.clk32k_src);
  1176. ret = -EINVAL;
  1177. goto err_reset;
  1178. }
  1179. for (i = 0; i < ARIZONA_MAX_MICBIAS; i++) {
  1180. if (!arizona->pdata.micbias[i].mV &&
  1181. !arizona->pdata.micbias[i].bypass)
  1182. continue;
  1183. /* Apply default for bypass mode */
  1184. if (!arizona->pdata.micbias[i].mV)
  1185. arizona->pdata.micbias[i].mV = 2800;
  1186. val = (arizona->pdata.micbias[i].mV - 1500) / 100;
  1187. val <<= ARIZONA_MICB1_LVL_SHIFT;
  1188. if (arizona->pdata.micbias[i].ext_cap)
  1189. val |= ARIZONA_MICB1_EXT_CAP;
  1190. if (arizona->pdata.micbias[i].discharge)
  1191. val |= ARIZONA_MICB1_DISCH;
  1192. if (arizona->pdata.micbias[i].soft_start)
  1193. val |= ARIZONA_MICB1_RATE;
  1194. if (arizona->pdata.micbias[i].bypass)
  1195. val |= ARIZONA_MICB1_BYPASS;
  1196. regmap_update_bits(arizona->regmap,
  1197. ARIZONA_MIC_BIAS_CTRL_1 + i,
  1198. ARIZONA_MICB1_LVL_MASK |
  1199. ARIZONA_MICB1_EXT_CAP |
  1200. ARIZONA_MICB1_DISCH |
  1201. ARIZONA_MICB1_BYPASS |
  1202. ARIZONA_MICB1_RATE, val);
  1203. }
  1204. pm_runtime_set_active(arizona->dev);
  1205. pm_runtime_enable(arizona->dev);
  1206. /* Set up for interrupts */
  1207. ret = arizona_irq_init(arizona);
  1208. if (ret != 0)
  1209. goto err_pm;
  1210. pm_runtime_set_autosuspend_delay(arizona->dev, 100);
  1211. pm_runtime_use_autosuspend(arizona->dev);
  1212. arizona_request_irq(arizona, ARIZONA_IRQ_CLKGEN_ERR, "CLKGEN error",
  1213. arizona_clkgen_err, arizona);
  1214. arizona_request_irq(arizona, ARIZONA_IRQ_OVERCLOCKED, "Overclocked",
  1215. arizona_overclocked, arizona);
  1216. arizona_request_irq(arizona, ARIZONA_IRQ_UNDERCLOCKED, "Underclocked",
  1217. arizona_underclocked, arizona);
  1218. ret = mfd_add_devices(arizona->dev, PLATFORM_DEVID_NONE,
  1219. subdevs, n_subdevs, NULL, 0, NULL);
  1220. if (ret) {
  1221. dev_err(arizona->dev, "Failed to add subdevices: %d\n", ret);
  1222. goto err_irq;
  1223. }
  1224. return 0;
  1225. err_irq:
  1226. arizona_irq_exit(arizona);
  1227. err_pm:
  1228. pm_runtime_disable(arizona->dev);
  1229. err_reset:
  1230. arizona_enable_reset(arizona);
  1231. regulator_disable(arizona->dcvdd);
  1232. err_enable:
  1233. regulator_bulk_disable(arizona->num_core_supplies,
  1234. arizona->core_supplies);
  1235. err_dcvdd:
  1236. regulator_put(arizona->dcvdd);
  1237. err_early:
  1238. mfd_remove_devices(dev);
  1239. return ret;
  1240. }
  1241. EXPORT_SYMBOL_GPL(arizona_dev_init);
  1242. int arizona_dev_exit(struct arizona *arizona)
  1243. {
  1244. disable_irq(arizona->irq);
  1245. pm_runtime_disable(arizona->dev);
  1246. regulator_disable(arizona->dcvdd);
  1247. regulator_put(arizona->dcvdd);
  1248. mfd_remove_devices(arizona->dev);
  1249. arizona_free_irq(arizona, ARIZONA_IRQ_UNDERCLOCKED, arizona);
  1250. arizona_free_irq(arizona, ARIZONA_IRQ_OVERCLOCKED, arizona);
  1251. arizona_free_irq(arizona, ARIZONA_IRQ_CLKGEN_ERR, arizona);
  1252. arizona_irq_exit(arizona);
  1253. arizona_enable_reset(arizona);
  1254. regulator_bulk_disable(arizona->num_core_supplies,
  1255. arizona->core_supplies);
  1256. return 0;
  1257. }
  1258. EXPORT_SYMBOL_GPL(arizona_dev_exit);