rk808.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. * MFD core driver for Rockchip RK808/RK818
  3. *
  4. * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
  5. *
  6. * Author: Chris Zhong <zyw@rock-chips.com>
  7. * Author: Zhang Qing <zhangqing@rock-chips.com>
  8. *
  9. * Copyright (C) 2016 PHYTEC Messtechnik GmbH
  10. *
  11. * Author: Wadim Egorov <w.egorov@phytec.de>
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms and conditions of the GNU General Public License,
  15. * version 2, as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope it will be useful, but WITHOUT
  18. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  20. * more details.
  21. */
  22. #include <linux/i2c.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/mfd/rk808.h>
  25. #include <linux/mfd/core.h>
  26. #include <linux/module.h>
  27. #include <linux/of_device.h>
  28. #include <linux/regmap.h>
  29. struct rk808_reg_data {
  30. int addr;
  31. int mask;
  32. int value;
  33. };
  34. static bool rk808_is_volatile_reg(struct device *dev, unsigned int reg)
  35. {
  36. /*
  37. * Notes:
  38. * - Technically the ROUND_30s bit makes RTC_CTRL_REG volatile, but
  39. * we don't use that feature. It's better to cache.
  40. * - It's unlikely we care that RK808_DEVCTRL_REG is volatile since
  41. * bits are cleared in case when we shutoff anyway, but better safe.
  42. */
  43. switch (reg) {
  44. case RK808_SECONDS_REG ... RK808_WEEKS_REG:
  45. case RK808_RTC_STATUS_REG:
  46. case RK808_VB_MON_REG:
  47. case RK808_THERMAL_REG:
  48. case RK808_DCDC_UV_STS_REG:
  49. case RK808_LDO_UV_STS_REG:
  50. case RK808_DCDC_PG_REG:
  51. case RK808_LDO_PG_REG:
  52. case RK808_DEVCTRL_REG:
  53. case RK808_INT_STS_REG1:
  54. case RK808_INT_STS_REG2:
  55. return true;
  56. }
  57. return false;
  58. }
  59. static const struct regmap_config rk818_regmap_config = {
  60. .reg_bits = 8,
  61. .val_bits = 8,
  62. .max_register = RK818_USB_CTRL_REG,
  63. .cache_type = REGCACHE_RBTREE,
  64. .volatile_reg = rk808_is_volatile_reg,
  65. };
  66. static const struct regmap_config rk808_regmap_config = {
  67. .reg_bits = 8,
  68. .val_bits = 8,
  69. .max_register = RK808_IO_POL_REG,
  70. .cache_type = REGCACHE_RBTREE,
  71. .volatile_reg = rk808_is_volatile_reg,
  72. };
  73. static struct resource rtc_resources[] = {
  74. {
  75. .start = RK808_IRQ_RTC_ALARM,
  76. .end = RK808_IRQ_RTC_ALARM,
  77. .flags = IORESOURCE_IRQ,
  78. }
  79. };
  80. static const struct mfd_cell rk808s[] = {
  81. { .name = "rk808-clkout", },
  82. { .name = "rk808-regulator", },
  83. {
  84. .name = "rk808-rtc",
  85. .num_resources = ARRAY_SIZE(rtc_resources),
  86. .resources = rtc_resources,
  87. },
  88. };
  89. static const struct mfd_cell rk818s[] = {
  90. { .name = "rk808-clkout", },
  91. { .name = "rk808-regulator", },
  92. {
  93. .name = "rk808-rtc",
  94. .num_resources = ARRAY_SIZE(rtc_resources),
  95. .resources = rtc_resources,
  96. },
  97. };
  98. static const struct rk808_reg_data rk808_pre_init_reg[] = {
  99. { RK808_BUCK3_CONFIG_REG, BUCK_ILMIN_MASK, BUCK_ILMIN_150MA },
  100. { RK808_BUCK4_CONFIG_REG, BUCK_ILMIN_MASK, BUCK_ILMIN_200MA },
  101. { RK808_BOOST_CONFIG_REG, BOOST_ILMIN_MASK, BOOST_ILMIN_100MA },
  102. { RK808_BUCK1_CONFIG_REG, BUCK1_RATE_MASK, BUCK_ILMIN_200MA },
  103. { RK808_BUCK2_CONFIG_REG, BUCK2_RATE_MASK, BUCK_ILMIN_200MA },
  104. { RK808_DCDC_UV_ACT_REG, BUCK_UV_ACT_MASK, BUCK_UV_ACT_DISABLE},
  105. { RK808_VB_MON_REG, MASK_ALL, VB_LO_ACT |
  106. VB_LO_SEL_3500MV },
  107. };
  108. static const struct rk808_reg_data rk818_pre_init_reg[] = {
  109. /* improve efficiency */
  110. { RK818_BUCK2_CONFIG_REG, BUCK2_RATE_MASK, BUCK_ILMIN_250MA },
  111. { RK818_BUCK4_CONFIG_REG, BUCK_ILMIN_MASK, BUCK_ILMIN_250MA },
  112. { RK818_BOOST_CONFIG_REG, BOOST_ILMIN_MASK, BOOST_ILMIN_100MA },
  113. { RK818_USB_CTRL_REG, RK818_USB_ILIM_SEL_MASK,
  114. RK818_USB_ILMIN_2000MA },
  115. /* close charger when usb lower then 3.4V */
  116. { RK818_USB_CTRL_REG, RK818_USB_CHG_SD_VSEL_MASK,
  117. (0x7 << 4) },
  118. /* no action when vref */
  119. { RK818_H5V_EN_REG, BIT(1), RK818_REF_RDY_CTRL },
  120. /* enable HDMI 5V */
  121. { RK818_H5V_EN_REG, BIT(0), RK818_H5V_EN },
  122. { RK808_VB_MON_REG, MASK_ALL, VB_LO_ACT |
  123. VB_LO_SEL_3500MV },
  124. };
  125. static const struct regmap_irq rk808_irqs[] = {
  126. /* INT_STS */
  127. [RK808_IRQ_VOUT_LO] = {
  128. .mask = RK808_IRQ_VOUT_LO_MSK,
  129. .reg_offset = 0,
  130. },
  131. [RK808_IRQ_VB_LO] = {
  132. .mask = RK808_IRQ_VB_LO_MSK,
  133. .reg_offset = 0,
  134. },
  135. [RK808_IRQ_PWRON] = {
  136. .mask = RK808_IRQ_PWRON_MSK,
  137. .reg_offset = 0,
  138. },
  139. [RK808_IRQ_PWRON_LP] = {
  140. .mask = RK808_IRQ_PWRON_LP_MSK,
  141. .reg_offset = 0,
  142. },
  143. [RK808_IRQ_HOTDIE] = {
  144. .mask = RK808_IRQ_HOTDIE_MSK,
  145. .reg_offset = 0,
  146. },
  147. [RK808_IRQ_RTC_ALARM] = {
  148. .mask = RK808_IRQ_RTC_ALARM_MSK,
  149. .reg_offset = 0,
  150. },
  151. [RK808_IRQ_RTC_PERIOD] = {
  152. .mask = RK808_IRQ_RTC_PERIOD_MSK,
  153. .reg_offset = 0,
  154. },
  155. /* INT_STS2 */
  156. [RK808_IRQ_PLUG_IN_INT] = {
  157. .mask = RK808_IRQ_PLUG_IN_INT_MSK,
  158. .reg_offset = 1,
  159. },
  160. [RK808_IRQ_PLUG_OUT_INT] = {
  161. .mask = RK808_IRQ_PLUG_OUT_INT_MSK,
  162. .reg_offset = 1,
  163. },
  164. };
  165. static const struct regmap_irq rk818_irqs[] = {
  166. /* INT_STS */
  167. [RK818_IRQ_VOUT_LO] = {
  168. .mask = RK818_IRQ_VOUT_LO_MSK,
  169. .reg_offset = 0,
  170. },
  171. [RK818_IRQ_VB_LO] = {
  172. .mask = RK818_IRQ_VB_LO_MSK,
  173. .reg_offset = 0,
  174. },
  175. [RK818_IRQ_PWRON] = {
  176. .mask = RK818_IRQ_PWRON_MSK,
  177. .reg_offset = 0,
  178. },
  179. [RK818_IRQ_PWRON_LP] = {
  180. .mask = RK818_IRQ_PWRON_LP_MSK,
  181. .reg_offset = 0,
  182. },
  183. [RK818_IRQ_HOTDIE] = {
  184. .mask = RK818_IRQ_HOTDIE_MSK,
  185. .reg_offset = 0,
  186. },
  187. [RK818_IRQ_RTC_ALARM] = {
  188. .mask = RK818_IRQ_RTC_ALARM_MSK,
  189. .reg_offset = 0,
  190. },
  191. [RK818_IRQ_RTC_PERIOD] = {
  192. .mask = RK818_IRQ_RTC_PERIOD_MSK,
  193. .reg_offset = 0,
  194. },
  195. [RK818_IRQ_USB_OV] = {
  196. .mask = RK818_IRQ_USB_OV_MSK,
  197. .reg_offset = 0,
  198. },
  199. /* INT_STS2 */
  200. [RK818_IRQ_PLUG_IN] = {
  201. .mask = RK818_IRQ_PLUG_IN_MSK,
  202. .reg_offset = 1,
  203. },
  204. [RK818_IRQ_PLUG_OUT] = {
  205. .mask = RK818_IRQ_PLUG_OUT_MSK,
  206. .reg_offset = 1,
  207. },
  208. [RK818_IRQ_CHG_OK] = {
  209. .mask = RK818_IRQ_CHG_OK_MSK,
  210. .reg_offset = 1,
  211. },
  212. [RK818_IRQ_CHG_TE] = {
  213. .mask = RK818_IRQ_CHG_TE_MSK,
  214. .reg_offset = 1,
  215. },
  216. [RK818_IRQ_CHG_TS1] = {
  217. .mask = RK818_IRQ_CHG_TS1_MSK,
  218. .reg_offset = 1,
  219. },
  220. [RK818_IRQ_TS2] = {
  221. .mask = RK818_IRQ_TS2_MSK,
  222. .reg_offset = 1,
  223. },
  224. [RK818_IRQ_CHG_CVTLIM] = {
  225. .mask = RK818_IRQ_CHG_CVTLIM_MSK,
  226. .reg_offset = 1,
  227. },
  228. [RK818_IRQ_DISCHG_ILIM] = {
  229. .mask = RK818_IRQ_DISCHG_ILIM_MSK,
  230. .reg_offset = 1,
  231. },
  232. };
  233. static struct regmap_irq_chip rk808_irq_chip = {
  234. .name = "rk808",
  235. .irqs = rk808_irqs,
  236. .num_irqs = ARRAY_SIZE(rk808_irqs),
  237. .num_regs = 2,
  238. .irq_reg_stride = 2,
  239. .status_base = RK808_INT_STS_REG1,
  240. .mask_base = RK808_INT_STS_MSK_REG1,
  241. .ack_base = RK808_INT_STS_REG1,
  242. .init_ack_masked = true,
  243. };
  244. static struct regmap_irq_chip rk818_irq_chip = {
  245. .name = "rk818",
  246. .irqs = rk818_irqs,
  247. .num_irqs = ARRAY_SIZE(rk818_irqs),
  248. .num_regs = 2,
  249. .irq_reg_stride = 2,
  250. .status_base = RK818_INT_STS_REG1,
  251. .mask_base = RK818_INT_STS_MSK_REG1,
  252. .ack_base = RK818_INT_STS_REG1,
  253. .init_ack_masked = true,
  254. };
  255. static struct i2c_client *rk808_i2c_client;
  256. static void rk808_device_shutdown(void)
  257. {
  258. int ret;
  259. struct rk808 *rk808 = i2c_get_clientdata(rk808_i2c_client);
  260. if (!rk808) {
  261. dev_warn(&rk808_i2c_client->dev,
  262. "have no rk808, so do nothing here\n");
  263. return;
  264. }
  265. ret = regmap_update_bits(rk808->regmap,
  266. RK808_DEVCTRL_REG,
  267. DEV_OFF_RST, DEV_OFF_RST);
  268. if (ret)
  269. dev_err(&rk808_i2c_client->dev, "power off error!\n");
  270. }
  271. static const struct of_device_id rk808_of_match[] = {
  272. { .compatible = "rockchip,rk808" },
  273. { .compatible = "rockchip,rk818" },
  274. { },
  275. };
  276. MODULE_DEVICE_TABLE(of, rk808_of_match);
  277. static int rk808_probe(struct i2c_client *client,
  278. const struct i2c_device_id *id)
  279. {
  280. struct device_node *np = client->dev.of_node;
  281. struct rk808 *rk808;
  282. const struct rk808_reg_data *pre_init_reg;
  283. const struct mfd_cell *cells;
  284. int nr_pre_init_regs;
  285. int nr_cells;
  286. int pm_off = 0;
  287. int ret;
  288. int i;
  289. rk808 = devm_kzalloc(&client->dev, sizeof(*rk808), GFP_KERNEL);
  290. if (!rk808)
  291. return -ENOMEM;
  292. rk808->variant = i2c_smbus_read_word_data(client, RK808_ID_MSB);
  293. if (rk808->variant < 0) {
  294. dev_err(&client->dev, "Failed to read the chip id at 0x%02x\n",
  295. RK808_ID_MSB);
  296. return rk808->variant;
  297. }
  298. dev_dbg(&client->dev, "Chip id: 0x%x\n", (unsigned int)rk808->variant);
  299. switch (rk808->variant) {
  300. case RK808_ID:
  301. rk808->regmap_cfg = &rk808_regmap_config;
  302. rk808->regmap_irq_chip = &rk808_irq_chip;
  303. pre_init_reg = rk808_pre_init_reg;
  304. nr_pre_init_regs = ARRAY_SIZE(rk808_pre_init_reg);
  305. cells = rk808s;
  306. nr_cells = ARRAY_SIZE(rk808s);
  307. break;
  308. case RK818_ID:
  309. rk808->regmap_cfg = &rk818_regmap_config;
  310. rk808->regmap_irq_chip = &rk818_irq_chip;
  311. pre_init_reg = rk818_pre_init_reg;
  312. nr_pre_init_regs = ARRAY_SIZE(rk818_pre_init_reg);
  313. cells = rk818s;
  314. nr_cells = ARRAY_SIZE(rk818s);
  315. break;
  316. default:
  317. dev_err(&client->dev, "Unsupported RK8XX ID %lu\n",
  318. rk808->variant);
  319. return -EINVAL;
  320. }
  321. rk808->i2c = client;
  322. i2c_set_clientdata(client, rk808);
  323. rk808->regmap = devm_regmap_init_i2c(client, rk808->regmap_cfg);
  324. if (IS_ERR(rk808->regmap)) {
  325. dev_err(&client->dev, "regmap initialization failed\n");
  326. return PTR_ERR(rk808->regmap);
  327. }
  328. if (!client->irq) {
  329. dev_err(&client->dev, "No interrupt support, no core IRQ\n");
  330. return -EINVAL;
  331. }
  332. ret = regmap_add_irq_chip(rk808->regmap, client->irq,
  333. IRQF_ONESHOT, -1,
  334. rk808->regmap_irq_chip, &rk808->irq_data);
  335. if (ret) {
  336. dev_err(&client->dev, "Failed to add irq_chip %d\n", ret);
  337. return ret;
  338. }
  339. for (i = 0; i < nr_pre_init_regs; i++) {
  340. ret = regmap_update_bits(rk808->regmap,
  341. pre_init_reg[i].addr,
  342. pre_init_reg[i].mask,
  343. pre_init_reg[i].value);
  344. if (ret) {
  345. dev_err(&client->dev,
  346. "0x%x write err\n",
  347. pre_init_reg[i].addr);
  348. return ret;
  349. }
  350. }
  351. ret = devm_mfd_add_devices(&client->dev, PLATFORM_DEVID_NONE,
  352. cells, nr_cells, NULL, 0,
  353. regmap_irq_get_domain(rk808->irq_data));
  354. if (ret) {
  355. dev_err(&client->dev, "failed to add MFD devices %d\n", ret);
  356. goto err_irq;
  357. }
  358. pm_off = of_property_read_bool(np,
  359. "rockchip,system-power-controller");
  360. if (pm_off && !pm_power_off) {
  361. rk808_i2c_client = client;
  362. pm_power_off = rk808_device_shutdown;
  363. }
  364. return 0;
  365. err_irq:
  366. regmap_del_irq_chip(client->irq, rk808->irq_data);
  367. return ret;
  368. }
  369. static int rk808_remove(struct i2c_client *client)
  370. {
  371. struct rk808 *rk808 = i2c_get_clientdata(client);
  372. regmap_del_irq_chip(client->irq, rk808->irq_data);
  373. pm_power_off = NULL;
  374. return 0;
  375. }
  376. static const struct i2c_device_id rk808_ids[] = {
  377. { "rk808" },
  378. { "rk818" },
  379. { },
  380. };
  381. MODULE_DEVICE_TABLE(i2c, rk808_ids);
  382. static struct i2c_driver rk808_i2c_driver = {
  383. .driver = {
  384. .name = "rk808",
  385. .of_match_table = rk808_of_match,
  386. },
  387. .probe = rk808_probe,
  388. .remove = rk808_remove,
  389. .id_table = rk808_ids,
  390. };
  391. module_i2c_driver(rk808_i2c_driver);
  392. MODULE_LICENSE("GPL");
  393. MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
  394. MODULE_AUTHOR("Zhang Qing <zhangqing@rock-chips.com>");
  395. MODULE_AUTHOR("Wadim Egorov <w.egorov@phytec.de>");
  396. MODULE_DESCRIPTION("RK808/RK818 PMIC driver");