da9052-i2c.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * I2C access for DA9052 PMICs.
  4. *
  5. * Copyright(c) 2011 Dialog Semiconductor Ltd.
  6. *
  7. * Author: David Dajun Chen <[email protected]>
  8. */
  9. #include <linux/device.h>
  10. #include <linux/module.h>
  11. #include <linux/input.h>
  12. #include <linux/mfd/core.h>
  13. #include <linux/i2c.h>
  14. #include <linux/err.h>
  15. #include <linux/mfd/da9052/da9052.h>
  16. #include <linux/mfd/da9052/reg.h>
  17. #ifdef CONFIG_OF
  18. #include <linux/of.h>
  19. #include <linux/of_device.h>
  20. #endif
  21. /* I2C safe register check */
  22. static inline bool i2c_safe_reg(unsigned char reg)
  23. {
  24. switch (reg) {
  25. case DA9052_STATUS_A_REG:
  26. case DA9052_STATUS_B_REG:
  27. case DA9052_STATUS_C_REG:
  28. case DA9052_STATUS_D_REG:
  29. case DA9052_ADC_RES_L_REG:
  30. case DA9052_ADC_RES_H_REG:
  31. case DA9052_VDD_RES_REG:
  32. case DA9052_ICHG_AV_REG:
  33. case DA9052_TBAT_RES_REG:
  34. case DA9052_ADCIN4_RES_REG:
  35. case DA9052_ADCIN5_RES_REG:
  36. case DA9052_ADCIN6_RES_REG:
  37. case DA9052_TJUNC_RES_REG:
  38. case DA9052_TSI_X_MSB_REG:
  39. case DA9052_TSI_Y_MSB_REG:
  40. case DA9052_TSI_LSB_REG:
  41. case DA9052_TSI_Z_MSB_REG:
  42. return true;
  43. default:
  44. return false;
  45. }
  46. }
  47. /*
  48. * There is an issue with DA9052 and DA9053_AA/BA/BB PMIC where the PMIC
  49. * gets lockup up or fails to respond following a system reset.
  50. * This fix is to follow any read or write with a dummy read to a safe
  51. * register.
  52. */
  53. static int da9052_i2c_fix(struct da9052 *da9052, unsigned char reg)
  54. {
  55. int val;
  56. switch (da9052->chip_id) {
  57. case DA9052:
  58. case DA9053_AA:
  59. case DA9053_BA:
  60. case DA9053_BB:
  61. /* A dummy read to a safe register address. */
  62. if (!i2c_safe_reg(reg))
  63. return regmap_read(da9052->regmap,
  64. DA9052_PARK_REGISTER,
  65. &val);
  66. break;
  67. case DA9053_BC:
  68. default:
  69. /*
  70. * For other chips parking of I2C register
  71. * to a safe place is not required.
  72. */
  73. break;
  74. }
  75. return 0;
  76. }
  77. /*
  78. * According to errata item 24, multiwrite mode should be avoided
  79. * in order to prevent register data corruption after power-down.
  80. */
  81. static int da9052_i2c_disable_multiwrite(struct da9052 *da9052)
  82. {
  83. int reg_val, ret;
  84. ret = regmap_read(da9052->regmap, DA9052_CONTROL_B_REG, &reg_val);
  85. if (ret < 0)
  86. return ret;
  87. if (!(reg_val & DA9052_CONTROL_B_WRITEMODE)) {
  88. reg_val |= DA9052_CONTROL_B_WRITEMODE;
  89. ret = regmap_write(da9052->regmap, DA9052_CONTROL_B_REG,
  90. reg_val);
  91. if (ret < 0)
  92. return ret;
  93. }
  94. return 0;
  95. }
  96. static const struct i2c_device_id da9052_i2c_id[] = {
  97. {"da9052", DA9052},
  98. {"da9053-aa", DA9053_AA},
  99. {"da9053-ba", DA9053_BA},
  100. {"da9053-bb", DA9053_BB},
  101. {"da9053-bc", DA9053_BC},
  102. {}
  103. };
  104. MODULE_DEVICE_TABLE(i2c, da9052_i2c_id);
  105. #ifdef CONFIG_OF
  106. static const struct of_device_id dialog_dt_ids[] = {
  107. { .compatible = "dlg,da9052", .data = &da9052_i2c_id[0] },
  108. { .compatible = "dlg,da9053-aa", .data = &da9052_i2c_id[1] },
  109. { .compatible = "dlg,da9053-ba", .data = &da9052_i2c_id[2] },
  110. { .compatible = "dlg,da9053-bb", .data = &da9052_i2c_id[3] },
  111. { .compatible = "dlg,da9053-bc", .data = &da9052_i2c_id[4] },
  112. { /* sentinel */ }
  113. };
  114. #endif
  115. static int da9052_i2c_probe(struct i2c_client *client,
  116. const struct i2c_device_id *id)
  117. {
  118. struct da9052 *da9052;
  119. int ret;
  120. da9052 = devm_kzalloc(&client->dev, sizeof(struct da9052), GFP_KERNEL);
  121. if (!da9052)
  122. return -ENOMEM;
  123. da9052->dev = &client->dev;
  124. da9052->chip_irq = client->irq;
  125. da9052->fix_io = da9052_i2c_fix;
  126. i2c_set_clientdata(client, da9052);
  127. da9052->regmap = devm_regmap_init_i2c(client, &da9052_regmap_config);
  128. if (IS_ERR(da9052->regmap)) {
  129. ret = PTR_ERR(da9052->regmap);
  130. dev_err(&client->dev, "Failed to allocate register map: %d\n",
  131. ret);
  132. return ret;
  133. }
  134. ret = da9052_i2c_disable_multiwrite(da9052);
  135. if (ret < 0)
  136. return ret;
  137. #ifdef CONFIG_OF
  138. if (!id)
  139. id = of_device_get_match_data(&client->dev);
  140. #endif
  141. if (!id) {
  142. ret = -ENODEV;
  143. dev_err(&client->dev, "id is null.\n");
  144. return ret;
  145. }
  146. return da9052_device_init(da9052, id->driver_data);
  147. }
  148. static void da9052_i2c_remove(struct i2c_client *client)
  149. {
  150. struct da9052 *da9052 = i2c_get_clientdata(client);
  151. da9052_device_exit(da9052);
  152. }
  153. static struct i2c_driver da9052_i2c_driver = {
  154. .probe = da9052_i2c_probe,
  155. .remove = da9052_i2c_remove,
  156. .id_table = da9052_i2c_id,
  157. .driver = {
  158. .name = "da9052",
  159. #ifdef CONFIG_OF
  160. .of_match_table = dialog_dt_ids,
  161. #endif
  162. },
  163. };
  164. static int __init da9052_i2c_init(void)
  165. {
  166. int ret;
  167. ret = i2c_add_driver(&da9052_i2c_driver);
  168. if (ret != 0) {
  169. pr_err("DA9052 I2C registration failed %d\n", ret);
  170. return ret;
  171. }
  172. return 0;
  173. }
  174. subsys_initcall(da9052_i2c_init);
  175. static void __exit da9052_i2c_exit(void)
  176. {
  177. i2c_del_driver(&da9052_i2c_driver);
  178. }
  179. module_exit(da9052_i2c_exit);
  180. MODULE_AUTHOR("David Dajun Chen <[email protected]>");
  181. MODULE_DESCRIPTION("I2C driver for Dialog DA9052 PMIC");
  182. MODULE_LICENSE("GPL");