aqt1000-core.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /* Copyright (c) 2011-2018, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/of_device.h>
  15. #include <linux/slab.h>
  16. #include <linux/ratelimit.h>
  17. #include <linux/mfd/core.h>
  18. #include <linux/delay.h>
  19. #include <linux/gpio.h>
  20. #include <linux/debugfs.h>
  21. #include <linux/i2c.h>
  22. #include <linux/regmap.h>
  23. #include <sound/soc.h>
  24. #include "../msm-cdc-pinctrl.h"
  25. #include "../msm-cdc-supply.h"
  26. #include "aqt1000-registers.h"
  27. #include "aqt1000-internal.h"
  28. #include "aqt1000.h"
  29. #include "aqt1000-utils.h"
  30. #include "aqt1000-irq.h"
  31. static int aqt1000_bringup(struct aqt1000 *aqt)
  32. {
  33. struct aqt1000_pdata *pdata;
  34. u8 clk_div = 0, mclk = 1;
  35. if (!aqt->regmap) {
  36. dev_err(aqt->dev, "%s: aqt regmap is NULL\n", __func__);
  37. return -EINVAL;
  38. }
  39. /* Bringup register write sequence */
  40. regmap_update_bits(aqt->regmap, AQT1000_BUCK_5V_CTRL_CCL_1, 0xF0, 0xF0);
  41. regmap_update_bits(aqt->regmap, AQT1000_BIAS_CCOMP_FINE_ADJ,
  42. 0xF0, 0x90);
  43. regmap_update_bits(aqt->regmap, AQT1000_ANA_BIAS, 0x80, 0x80);
  44. regmap_update_bits(aqt->regmap, AQT1000_ANA_BIAS, 0x40, 0x40);
  45. /* Added 1msec sleep as per HW requirement */
  46. usleep_range(1000, 1010);
  47. regmap_update_bits(aqt->regmap, AQT1000_ANA_BIAS, 0x40, 0x00);
  48. clk_div = 0x04; /* Assumption is CLK DIV 2 */
  49. pdata = dev_get_platdata(aqt->dev);
  50. if (pdata) {
  51. if (pdata->mclk_rate == AQT1000_CLK_12P288MHZ)
  52. mclk = 0;
  53. clk_div = (((pdata->ext_clk_rate / pdata->mclk_rate) >> 1)
  54. << 2);
  55. }
  56. regmap_update_bits(aqt->regmap, AQT1000_CHIP_CFG0_CLK_CFG_MCLK,
  57. 0x03, mclk);
  58. regmap_update_bits(aqt->regmap, AQT1000_CLK_SYS_MCLK1_PRG,
  59. 0x0C, clk_div);
  60. /* Source clock enable */
  61. regmap_update_bits(aqt->regmap, AQT1000_CLK_SYS_MCLK1_PRG, 0x02, 0x02);
  62. /* Ungate the source clock */
  63. regmap_update_bits(aqt->regmap, AQT1000_CLK_SYS_MCLK1_PRG, 0x10, 0x10);
  64. /* Set the I2S_HS_CLK reference to CLK DIV 2 */
  65. regmap_update_bits(aqt->regmap, AQT1000_CLK_SYS_MCLK2_I2S_HS_CLK_PRG,
  66. 0x60, 0x20);
  67. /* Set the PLL preset to CLK9P6M_IN_12P288M_OUT */
  68. regmap_update_bits(aqt->regmap, AQT1000_CLK_SYS_PLL_PRESET, 0x0F, 0x02);
  69. /* Enable clock PLL */
  70. regmap_update_bits(aqt->regmap, AQT1000_CLK_SYS_PLL_ENABLES,
  71. 0x01, 0x01);
  72. /* Add 100usec delay as per HW requirement */
  73. usleep_range(100, 110);
  74. /* Set AQT to I2S Master */
  75. regmap_update_bits(aqt->regmap, AQT1000_I2S_I2S_0_CTL, 0x02, 0x02);
  76. /* Enable I2S HS clock */
  77. regmap_update_bits(aqt->regmap, AQT1000_CLK_SYS_MCLK2_I2S_HS_CLK_PRG,
  78. 0x01, 0x01);
  79. /* Codec digital reset */
  80. regmap_update_bits(aqt->regmap, AQT1000_CHIP_CFG0_RST_CTL, 0x01, 0x01);
  81. return 0;
  82. }
  83. static int aqt1000_device_init(struct aqt1000 *aqt)
  84. {
  85. int ret = 0;
  86. mutex_init(&aqt->io_lock);
  87. mutex_init(&aqt->xfer_lock);
  88. mutex_init(&aqt->cdc_bg_clk_lock);
  89. mutex_init(&aqt->master_bias_lock);
  90. ret = aqt1000_bringup(aqt);
  91. if (ret) {
  92. ret = -EPROBE_DEFER;
  93. goto done;
  94. }
  95. ret = aqt_irq_init(aqt);
  96. if (ret)
  97. goto done;
  98. return ret;
  99. done:
  100. mutex_destroy(&aqt->io_lock);
  101. mutex_destroy(&aqt->xfer_lock);
  102. mutex_destroy(&aqt->cdc_bg_clk_lock);
  103. mutex_destroy(&aqt->master_bias_lock);
  104. return ret;
  105. }
  106. static int aqt1000_i2c_write(struct aqt1000 *aqt1000, unsigned short reg,
  107. void *val, int bytes)
  108. {
  109. struct i2c_msg *msg;
  110. int ret = 0;
  111. u8 reg_addr = 0;
  112. u8 data[bytes + 1];
  113. struct aqt1000_i2c *aqt1000_i2c;
  114. u8 *value = (u8 *)val;
  115. aqt1000_i2c = &aqt1000->i2c_dev;
  116. if (aqt1000_i2c == NULL || aqt1000_i2c->client == NULL) {
  117. pr_err("%s: Failed to get device info\n", __func__);
  118. return -ENODEV;
  119. }
  120. reg_addr = (u8)reg;
  121. msg = &aqt1000_i2c->xfer_msg[0];
  122. msg->addr = aqt1000_i2c->client->addr;
  123. msg->len = bytes + 1;
  124. msg->flags = 0;
  125. data[0] = reg;
  126. data[1] = *value;
  127. msg->buf = data;
  128. ret = i2c_transfer(aqt1000_i2c->client->adapter,
  129. aqt1000_i2c->xfer_msg, 1);
  130. /* Try again if the write fails */
  131. if (ret != 1) {
  132. ret = i2c_transfer(aqt1000_i2c->client->adapter,
  133. aqt1000_i2c->xfer_msg, 1);
  134. if (ret != 1) {
  135. dev_err(aqt1000->dev,
  136. "%s: I2C write failed, reg: 0x%x ret: %d\n",
  137. __func__, reg, ret);
  138. return ret;
  139. }
  140. }
  141. dev_dbg(aqt1000->dev, "%s: write success register = %x val = %x\n",
  142. __func__, reg, data[1]);
  143. return 0;
  144. }
  145. static int aqt1000_i2c_read(struct aqt1000 *aqt1000, unsigned short reg,
  146. void *dst, int bytes)
  147. {
  148. struct i2c_msg *msg;
  149. int ret = 0;
  150. u8 reg_addr = 0;
  151. struct aqt1000_i2c *aqt1000_i2c;
  152. u8 i = 0;
  153. unsigned char *dest = (unsigned char *)dst;
  154. aqt1000_i2c = &aqt1000->i2c_dev;
  155. if (aqt1000_i2c == NULL || aqt1000_i2c->client == NULL) {
  156. pr_err("%s: Failed to get device info\n", __func__);
  157. return -ENODEV;
  158. }
  159. for (i = 0; i < bytes; i++) {
  160. reg_addr = (u8)reg++;
  161. msg = &aqt1000_i2c->xfer_msg[0];
  162. msg->addr = aqt1000_i2c->client->addr;
  163. msg->len = 1;
  164. msg->flags = 0;
  165. msg->buf = &reg_addr;
  166. msg = &aqt1000_i2c->xfer_msg[1];
  167. msg->addr = aqt1000_i2c->client->addr;
  168. msg->len = 1;
  169. msg->flags = I2C_M_RD;
  170. msg->buf = dest++;
  171. ret = i2c_transfer(aqt1000_i2c->client->adapter,
  172. aqt1000_i2c->xfer_msg, 2);
  173. /* Try again if read fails first time */
  174. if (ret != 2) {
  175. ret = i2c_transfer(aqt1000_i2c->client->adapter,
  176. aqt1000_i2c->xfer_msg, 2);
  177. if (ret != 2) {
  178. dev_err(aqt1000->dev,
  179. "%s: I2C read failed, reg: 0x%x\n",
  180. __func__, reg);
  181. return ret;
  182. }
  183. }
  184. }
  185. return 0;
  186. }
  187. static int aqt1000_reset(struct device *dev)
  188. {
  189. struct aqt1000 *aqt1000;
  190. int rc = 0;
  191. if (!dev)
  192. return -ENODEV;
  193. aqt1000 = dev_get_drvdata(dev);
  194. if (!aqt1000)
  195. return -EINVAL;
  196. if (!aqt1000->aqt_rst_np) {
  197. dev_err(dev, "%s: reset gpio device node not specified\n",
  198. __func__);
  199. return -EINVAL;
  200. }
  201. if (!msm_cdc_pinctrl_get_state(aqt1000->aqt_rst_np)) {
  202. rc = msm_cdc_pinctrl_select_sleep_state(aqt1000->aqt_rst_np);
  203. if (rc) {
  204. dev_err(dev, "%s: aqt sleep state request fail!\n",
  205. __func__);
  206. return rc;
  207. }
  208. /* 20ms sleep required after pulling the reset gpio to LOW */
  209. msleep(20);
  210. rc = msm_cdc_pinctrl_select_active_state(aqt1000->aqt_rst_np);
  211. if (rc) {
  212. dev_err(dev,
  213. "%s: aqt active state request fail, ret: %d\n",
  214. __func__, rc);
  215. return rc;
  216. }
  217. /* 20ms sleep required after pulling the reset gpio to HIGH */
  218. msleep(20);
  219. }
  220. return rc;
  221. }
  222. static int aqt1000_read_of_property_u32(struct device *dev, const char *name,
  223. u32 *val)
  224. {
  225. int rc = 0;
  226. rc = of_property_read_u32(dev->of_node, name, val);
  227. if (rc)
  228. dev_err(dev, "%s: Looking up %s property in node %s failed",
  229. __func__, name, dev->of_node->full_name);
  230. return rc;
  231. }
  232. static void aqt1000_dt_parse_micbias_info(struct device *dev,
  233. struct aqt1000_micbias_setting *mb)
  234. {
  235. u32 prop_val;
  236. int rc;
  237. if (of_find_property(dev->of_node, "qcom,cdc-micbias-ldoh-v", NULL)) {
  238. rc = aqt1000_read_of_property_u32(dev,
  239. "qcom,cdc-micbias-ldoh-v",
  240. &prop_val);
  241. if (!rc)
  242. mb->ldoh_v = (u8)prop_val;
  243. }
  244. /* MB1 */
  245. if (of_find_property(dev->of_node, "qcom,cdc-micbias-cfilt1-mv",
  246. NULL)) {
  247. rc = aqt1000_read_of_property_u32(dev,
  248. "qcom,cdc-micbias-cfilt1-mv",
  249. &prop_val);
  250. if (!rc)
  251. mb->cfilt1_mv = prop_val;
  252. rc = aqt1000_read_of_property_u32(dev,
  253. "qcom,cdc-micbias1-cfilt-sel",
  254. &prop_val);
  255. if (!rc)
  256. mb->bias1_cfilt_sel = (u8)prop_val;
  257. } else if (of_find_property(dev->of_node, "qcom,cdc-micbias1-mv",
  258. NULL)) {
  259. rc = aqt1000_read_of_property_u32(dev,
  260. "qcom,cdc-micbias1-mv",
  261. &prop_val);
  262. if (!rc)
  263. mb->micb1_mv = prop_val;
  264. } else {
  265. dev_info(dev, "%s: Micbias1 DT property not found\n",
  266. __func__);
  267. }
  268. /* Print micbias info */
  269. dev_dbg(dev, "%s: ldoh_v %u cfilt1_mv %u micb1_mv %u \n", __func__,
  270. (u32)mb->ldoh_v, (u32)mb->cfilt1_mv, (u32)mb->micb1_mv);
  271. }
  272. static struct aqt1000_pdata *aqt1000_populate_dt_data(struct device *dev)
  273. {
  274. struct aqt1000_pdata *pdata;
  275. u32 prop_val;
  276. if (!dev || !dev->of_node)
  277. return NULL;
  278. pdata = devm_kzalloc(dev, sizeof(struct aqt1000_pdata),
  279. GFP_KERNEL);
  280. if (!pdata)
  281. return NULL;
  282. /* Parse power supplies */
  283. msm_cdc_get_power_supplies(dev, &pdata->regulator,
  284. &pdata->num_supplies);
  285. if (!pdata->regulator || (pdata->num_supplies <= 0)) {
  286. dev_err(dev, "%s: no power supplies defined for codec\n",
  287. __func__);
  288. goto err_power_sup;
  289. }
  290. /* Parse micbias info */
  291. aqt1000_dt_parse_micbias_info(dev, &pdata->micbias);
  292. pdata->aqt_rst_np = of_parse_phandle(dev->of_node,
  293. "qcom,aqt-rst-gpio-node", 0);
  294. if (!pdata->aqt_rst_np) {
  295. dev_err(dev, "%s: Looking up %s property in node %s failed\n",
  296. __func__, "qcom,aqt-rst-gpio-node",
  297. dev->of_node->full_name);
  298. goto err_parse_dt_prop;
  299. }
  300. if (!(aqt1000_read_of_property_u32(dev, "qcom,cdc-ext-clk-rate",
  301. &prop_val)))
  302. pdata->ext_clk_rate = prop_val;
  303. if (pdata->ext_clk_rate != AQT1000_CLK_24P576MHZ &&
  304. pdata->ext_clk_rate != AQT1000_CLK_19P2MHZ &&
  305. pdata->ext_clk_rate != AQT1000_CLK_12P288MHZ) {
  306. /* Use the default ext_clk_rate if the DT value is wrong */
  307. pdata->ext_clk_rate = AQT1000_CLK_9P6MHZ;
  308. }
  309. prop_val = 0;
  310. if (!(aqt1000_read_of_property_u32(dev, "qcom,cdc-mclk-clk-rate",
  311. &prop_val)))
  312. pdata->mclk_rate = prop_val;
  313. if (pdata->mclk_rate != AQT1000_CLK_9P6MHZ &&
  314. pdata->mclk_rate != AQT1000_CLK_12P288MHZ) {
  315. dev_err(dev, "%s: Invalid mclk_rate = %u\n", __func__,
  316. pdata->mclk_rate);
  317. goto err_parse_dt_prop;
  318. }
  319. if (pdata->ext_clk_rate % pdata->mclk_rate) {
  320. dev_err(dev,
  321. "%s: Invalid clock group, ext_clk = %d mclk = %d\n",
  322. __func__, pdata->ext_clk_rate, pdata->mclk_rate);
  323. goto err_parse_dt_prop;
  324. }
  325. return pdata;
  326. err_parse_dt_prop:
  327. devm_kfree(dev, pdata->regulator);
  328. pdata->regulator = NULL;
  329. pdata->num_supplies = 0;
  330. err_power_sup:
  331. devm_kfree(dev, pdata);
  332. return NULL;
  333. }
  334. static int aqt1000_bringdown(struct device *dev)
  335. {
  336. /* No sequence for teardown */
  337. return 0;
  338. }
  339. static void aqt1000_device_exit(struct aqt1000 *aqt)
  340. {
  341. aqt_irq_exit(aqt);
  342. aqt1000_bringdown(aqt->dev);
  343. mutex_destroy(&aqt->io_lock);
  344. mutex_destroy(&aqt->xfer_lock);
  345. mutex_destroy(&aqt->cdc_bg_clk_lock);
  346. mutex_destroy(&aqt->master_bias_lock);
  347. }
  348. static int aqt1000_i2c_probe(struct i2c_client *client,
  349. const struct i2c_device_id *id)
  350. {
  351. struct aqt1000 *aqt1000 = NULL;
  352. struct aqt1000_pdata *pdata = NULL;
  353. int ret = 0;
  354. pdata = aqt1000_populate_dt_data(&client->dev);
  355. if (!pdata) {
  356. dev_err(&client->dev,
  357. "%s: Fail to obtain pdata from device tree\n",
  358. __func__);
  359. ret = -EINVAL;
  360. goto fail;
  361. }
  362. client->dev.platform_data = pdata;
  363. aqt1000 = devm_kzalloc(&client->dev, sizeof(struct aqt1000),
  364. GFP_KERNEL);
  365. if (!aqt1000) {
  366. ret = -ENOMEM;
  367. goto fail;
  368. }
  369. aqt1000->regmap = aqt1000_regmap_init(&client->dev,
  370. &aqt1000_regmap_config);
  371. if (IS_ERR(aqt1000->regmap)) {
  372. ret = PTR_ERR(aqt1000->regmap);
  373. dev_err(&client->dev,
  374. "%s: Failed to init register map: %d\n",
  375. __func__, ret);
  376. goto fail;
  377. }
  378. aqt1000->aqt_rst_np = pdata->aqt_rst_np;
  379. if (!aqt1000->aqt_rst_np) {
  380. dev_err(&client->dev, "%s: pinctrl not used for rst_n\n",
  381. __func__);
  382. ret = -EINVAL;
  383. goto fail;
  384. }
  385. if (i2c_check_functionality(client->adapter,
  386. I2C_FUNC_I2C) == 0) {
  387. dev_dbg(&client->dev, "%s: can't talk I2C?\n", __func__);
  388. ret = -EIO;
  389. goto fail;
  390. }
  391. dev_set_drvdata(&client->dev, aqt1000);
  392. aqt1000->dev = &client->dev;
  393. aqt1000->dev_up = true;
  394. aqt1000->mclk_rate = pdata->mclk_rate;
  395. aqt1000->num_of_supplies = pdata->num_supplies;
  396. ret = msm_cdc_init_supplies(aqt1000->dev, &aqt1000->supplies,
  397. pdata->regulator,
  398. pdata->num_supplies);
  399. if (!aqt1000->supplies) {
  400. dev_err(aqt1000->dev, "%s: Cannot init aqt supplies\n",
  401. __func__);
  402. goto err_codec;
  403. }
  404. ret = msm_cdc_enable_static_supplies(aqt1000->dev,
  405. aqt1000->supplies,
  406. pdata->regulator,
  407. pdata->num_supplies);
  408. if (ret) {
  409. dev_err(aqt1000->dev, "%s: aqt static supply enable failed!\n",
  410. __func__);
  411. goto err_codec;
  412. }
  413. /* 5 usec sleep is needed as per HW requirement */
  414. usleep_range(5, 10);
  415. ret = aqt1000_reset(aqt1000->dev);
  416. if (ret) {
  417. dev_err(aqt1000->dev, "%s: Codec reset failed\n", __func__);
  418. goto err_supplies;
  419. }
  420. aqt1000->i2c_dev.client = client;
  421. aqt1000->read_dev = aqt1000_i2c_read;
  422. aqt1000->write_dev = aqt1000_i2c_write;
  423. ret = aqt1000_device_init(aqt1000);
  424. if (ret) {
  425. pr_err("%s: error, initializing device failed (%d)\n",
  426. __func__, ret);
  427. goto err_supplies;
  428. }
  429. ret = aqt_register_codec(&client->dev);
  430. if (ret) {
  431. dev_err(aqt1000->dev, "%s: Codec registration failed\n",
  432. __func__);
  433. goto err_cdc_register;
  434. }
  435. return ret;
  436. err_cdc_register:
  437. aqt1000_device_exit(aqt1000);
  438. err_supplies:
  439. msm_cdc_release_supplies(aqt1000->dev, aqt1000->supplies,
  440. pdata->regulator,
  441. pdata->num_supplies);
  442. pdata->regulator = NULL;
  443. pdata->num_supplies = 0;
  444. err_codec:
  445. devm_kfree(&client->dev, aqt1000);
  446. dev_set_drvdata(&client->dev, NULL);
  447. fail:
  448. return ret;
  449. }
  450. static int aqt1000_i2c_remove(struct i2c_client *client)
  451. {
  452. struct aqt1000 *aqt;
  453. struct aqt1000_pdata *pdata = client->dev.platform_data;
  454. aqt = dev_get_drvdata(&client->dev);
  455. msm_cdc_release_supplies(aqt->dev, aqt->supplies,
  456. pdata->regulator,
  457. pdata->num_supplies);
  458. aqt1000_device_exit(aqt);
  459. dev_set_drvdata(&client->dev, NULL);
  460. return 0;
  461. }
  462. #ifdef CONFIG_PM_SLEEP
  463. static int aqt1000_i2c_resume(struct device *dev)
  464. {
  465. pr_debug("%s system resume\n", __func__);
  466. return 0;
  467. }
  468. static int aqt1000_i2c_suspend(struct device *dev)
  469. {
  470. pr_debug("%s system suspend\n", __func__);
  471. return 0;
  472. }
  473. #endif
  474. static struct i2c_device_id aqt1000_id_table[] = {
  475. {"aqt1000-i2c", 0},
  476. {}
  477. };
  478. MODULE_DEVICE_TABLE(i2c, aqt1000_id_table);
  479. static const struct dev_pm_ops aqt1000_i2c_pm_ops = {
  480. .suspend = aqt1000_i2c_suspend,
  481. .resume = aqt1000_i2c_resume,
  482. };
  483. static const struct of_device_id aqt_match_table[] = {
  484. {.compatible = "qcom,aqt1000-i2c-codec"},
  485. {}
  486. };
  487. MODULE_DEVICE_TABLE(of, aqt_match_table);
  488. static struct i2c_driver aqt1000_i2c_driver = {
  489. .driver = {
  490. .owner = THIS_MODULE,
  491. .name = "aqt1000-i2c-codec",
  492. #ifdef CONFIG_PM_SLEEP
  493. .pm = &aqt1000_i2c_pm_ops,
  494. #endif
  495. .of_match_table = aqt_match_table,
  496. },
  497. .id_table = aqt1000_id_table,
  498. .probe = aqt1000_i2c_probe,
  499. .remove = aqt1000_i2c_remove,
  500. };
  501. static int __init aqt1000_init(void)
  502. {
  503. return i2c_add_driver(&aqt1000_i2c_driver);
  504. }
  505. module_init(aqt1000_init);
  506. static void __exit aqt1000_exit(void)
  507. {
  508. i2c_del_driver(&aqt1000_i2c_driver);
  509. }
  510. module_exit(aqt1000_exit);
  511. MODULE_DESCRIPTION("AQT1000 Codec driver");
  512. MODULE_LICENSE("GPL v2");