max20086-regulator.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. //
  3. // max20086-regulator.c - MAX20086-MAX20089 camera power protector driver
  4. //
  5. // Copyright (C) 2022 Laurent Pinchart <[email protected]>
  6. // Copyright (C) 2018 Avnet, Inc.
  7. #include <linux/err.h>
  8. #include <linux/gpio.h>
  9. #include <linux/gpio/consumer.h>
  10. #include <linux/i2c.h>
  11. #include <linux/module.h>
  12. #include <linux/regmap.h>
  13. #include <linux/regulator/driver.h>
  14. #include <linux/regulator/machine.h>
  15. #include <linux/regulator/of_regulator.h>
  16. #include <linux/slab.h>
  17. /* Register Offset */
  18. #define MAX20086_REG_MASK 0x00
  19. #define MAX20086_REG_CONFIG 0x01
  20. #define MAX20086_REG_ID 0x02
  21. #define MAX20086_REG_STAT1 0x03
  22. #define MAX20086_REG_STAT2_L 0x04
  23. #define MAX20086_REG_STAT2_H 0x05
  24. #define MAX20086_REG_ADC1 0x06
  25. #define MAX20086_REG_ADC2 0x07
  26. #define MAX20086_REG_ADC3 0x08
  27. #define MAX20086_REG_ADC4 0x09
  28. /* DEVICE IDs */
  29. #define MAX20086_DEVICE_ID_MAX20086 0x40
  30. #define MAX20086_DEVICE_ID_MAX20087 0x20
  31. #define MAX20086_DEVICE_ID_MAX20088 0x10
  32. #define MAX20086_DEVICE_ID_MAX20089 0x00
  33. #define DEVICE_ID_MASK 0xf0
  34. /* Register bits */
  35. #define MAX20086_EN_MASK 0x0f
  36. #define MAX20086_EN_OUT1 0x01
  37. #define MAX20086_EN_OUT2 0x02
  38. #define MAX20086_EN_OUT3 0x04
  39. #define MAX20086_EN_OUT4 0x08
  40. #define MAX20086_INT_DISABLE_ALL 0x3f
  41. #define MAX20086_MAX_REGULATORS 4
  42. struct max20086_chip_info {
  43. u8 id;
  44. unsigned int num_outputs;
  45. };
  46. struct max20086_regulator {
  47. struct device_node *of_node;
  48. struct regulator_init_data *init_data;
  49. const struct regulator_desc *desc;
  50. struct regulator_dev *rdev;
  51. };
  52. struct max20086 {
  53. struct device *dev;
  54. struct regmap *regmap;
  55. struct gpio_desc *ena_gpiod;
  56. const struct max20086_chip_info *info;
  57. struct max20086_regulator regulators[MAX20086_MAX_REGULATORS];
  58. };
  59. static const struct regulator_ops max20086_buck_ops = {
  60. .enable = regulator_enable_regmap,
  61. .disable = regulator_disable_regmap,
  62. .is_enabled = regulator_is_enabled_regmap,
  63. };
  64. #define MAX20086_REGULATOR_DESC(n) \
  65. { \
  66. .name = "OUT"#n, \
  67. .supply_name = "in", \
  68. .id = (n) - 1, \
  69. .ops = &max20086_buck_ops, \
  70. .type = REGULATOR_VOLTAGE, \
  71. .owner = THIS_MODULE, \
  72. .enable_reg = MAX20086_REG_CONFIG, \
  73. .enable_mask = 1 << ((n) - 1), \
  74. .enable_val = 1 << ((n) - 1), \
  75. .disable_val = 0, \
  76. }
  77. static const char * const max20086_output_names[] = {
  78. "OUT1",
  79. "OUT2",
  80. "OUT3",
  81. "OUT4",
  82. };
  83. static const struct regulator_desc max20086_regulators[] = {
  84. MAX20086_REGULATOR_DESC(1),
  85. MAX20086_REGULATOR_DESC(2),
  86. MAX20086_REGULATOR_DESC(3),
  87. MAX20086_REGULATOR_DESC(4),
  88. };
  89. static int max20086_regulators_register(struct max20086 *chip)
  90. {
  91. unsigned int i;
  92. for (i = 0; i < chip->info->num_outputs; i++) {
  93. struct max20086_regulator *reg = &chip->regulators[i];
  94. struct regulator_config config = { };
  95. struct regulator_dev *rdev;
  96. config.dev = chip->dev;
  97. config.init_data = reg->init_data;
  98. config.driver_data = chip;
  99. config.of_node = reg->of_node;
  100. config.regmap = chip->regmap;
  101. config.ena_gpiod = chip->ena_gpiod;
  102. rdev = devm_regulator_register(chip->dev, reg->desc, &config);
  103. if (IS_ERR(rdev)) {
  104. dev_err(chip->dev,
  105. "Failed to register regulator output %s\n",
  106. reg->desc->name);
  107. return PTR_ERR(rdev);
  108. }
  109. reg->rdev = rdev;
  110. }
  111. return 0;
  112. }
  113. static int max20086_parse_regulators_dt(struct max20086 *chip, bool *boot_on)
  114. {
  115. struct of_regulator_match matches[MAX20086_MAX_REGULATORS] = { };
  116. struct device_node *node;
  117. unsigned int i;
  118. int ret;
  119. node = of_get_child_by_name(chip->dev->of_node, "regulators");
  120. if (!node) {
  121. dev_err(chip->dev, "regulators node not found\n");
  122. return -ENODEV;
  123. }
  124. for (i = 0; i < chip->info->num_outputs; ++i)
  125. matches[i].name = max20086_output_names[i];
  126. ret = of_regulator_match(chip->dev, node, matches,
  127. chip->info->num_outputs);
  128. of_node_put(node);
  129. if (ret < 0) {
  130. dev_err(chip->dev, "Failed to match regulators\n");
  131. return -EINVAL;
  132. }
  133. *boot_on = false;
  134. for (i = 0; i < chip->info->num_outputs; i++) {
  135. struct max20086_regulator *reg = &chip->regulators[i];
  136. reg->init_data = matches[i].init_data;
  137. reg->of_node = matches[i].of_node;
  138. reg->desc = &max20086_regulators[i];
  139. if (reg->init_data) {
  140. if (reg->init_data->constraints.always_on ||
  141. reg->init_data->constraints.boot_on)
  142. *boot_on = true;
  143. }
  144. }
  145. return 0;
  146. }
  147. static int max20086_detect(struct max20086 *chip)
  148. {
  149. unsigned int data;
  150. int ret;
  151. ret = regmap_read(chip->regmap, MAX20086_REG_ID, &data);
  152. if (ret < 0) {
  153. dev_err(chip->dev, "Failed to read DEVICE_ID reg: %d\n", ret);
  154. return ret;
  155. }
  156. if ((data & DEVICE_ID_MASK) != chip->info->id) {
  157. dev_err(chip->dev, "Invalid device ID 0x%02x\n", data);
  158. return -ENXIO;
  159. }
  160. return 0;
  161. }
  162. static bool max20086_gen_is_writeable_reg(struct device *dev, unsigned int reg)
  163. {
  164. switch (reg) {
  165. case MAX20086_REG_MASK:
  166. case MAX20086_REG_CONFIG:
  167. return true;
  168. default:
  169. return false;
  170. }
  171. }
  172. static const struct regmap_config max20086_regmap_config = {
  173. .reg_bits = 8,
  174. .val_bits = 8,
  175. .writeable_reg = max20086_gen_is_writeable_reg,
  176. .max_register = 0x9,
  177. .cache_type = REGCACHE_NONE,
  178. };
  179. static int max20086_i2c_probe(struct i2c_client *i2c)
  180. {
  181. struct max20086 *chip;
  182. enum gpiod_flags flags;
  183. bool boot_on;
  184. int ret;
  185. chip = devm_kzalloc(&i2c->dev, sizeof(*chip), GFP_KERNEL);
  186. if (!chip)
  187. return -ENOMEM;
  188. chip->dev = &i2c->dev;
  189. chip->info = device_get_match_data(chip->dev);
  190. i2c_set_clientdata(i2c, chip);
  191. chip->regmap = devm_regmap_init_i2c(i2c, &max20086_regmap_config);
  192. if (IS_ERR(chip->regmap)) {
  193. ret = PTR_ERR(chip->regmap);
  194. dev_err(chip->dev, "Failed to allocate register map: %d\n", ret);
  195. return ret;
  196. }
  197. ret = max20086_parse_regulators_dt(chip, &boot_on);
  198. if (ret < 0)
  199. return ret;
  200. ret = max20086_detect(chip);
  201. if (ret < 0)
  202. return ret;
  203. /* Until IRQ support is added, just disable all interrupts. */
  204. ret = regmap_update_bits(chip->regmap, MAX20086_REG_MASK,
  205. MAX20086_INT_DISABLE_ALL,
  206. MAX20086_INT_DISABLE_ALL);
  207. if (ret < 0) {
  208. dev_err(chip->dev, "Failed to disable interrupts: %d\n", ret);
  209. return ret;
  210. }
  211. /*
  212. * Get the enable GPIO. If any of the outputs is marked as being
  213. * enabled at boot, request the GPIO with an initial high state to
  214. * avoid disabling outputs that may have been turned on by the boot
  215. * loader. Otherwise, request it with a low state to enter lower-power
  216. * shutdown.
  217. */
  218. flags = boot_on ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
  219. chip->ena_gpiod = devm_gpiod_get(chip->dev, "enable", flags);
  220. if (IS_ERR(chip->ena_gpiod)) {
  221. ret = PTR_ERR(chip->ena_gpiod);
  222. dev_err(chip->dev, "Failed to get enable GPIO: %d\n", ret);
  223. return ret;
  224. }
  225. ret = max20086_regulators_register(chip);
  226. if (ret < 0) {
  227. dev_err(chip->dev, "Failed to register regulators: %d\n", ret);
  228. return ret;
  229. }
  230. return 0;
  231. }
  232. static const struct i2c_device_id max20086_i2c_id[] = {
  233. { "max20086" },
  234. { "max20087" },
  235. { "max20088" },
  236. { "max20089" },
  237. { /* Sentinel */ },
  238. };
  239. MODULE_DEVICE_TABLE(i2c, max20086_i2c_id);
  240. static const struct of_device_id max20086_dt_ids[] = {
  241. {
  242. .compatible = "maxim,max20086",
  243. .data = &(const struct max20086_chip_info) {
  244. .id = MAX20086_DEVICE_ID_MAX20086,
  245. .num_outputs = 4,
  246. }
  247. }, {
  248. .compatible = "maxim,max20087",
  249. .data = &(const struct max20086_chip_info) {
  250. .id = MAX20086_DEVICE_ID_MAX20087,
  251. .num_outputs = 4,
  252. }
  253. }, {
  254. .compatible = "maxim,max20088",
  255. .data = &(const struct max20086_chip_info) {
  256. .id = MAX20086_DEVICE_ID_MAX20088,
  257. .num_outputs = 2,
  258. }
  259. }, {
  260. .compatible = "maxim,max20089",
  261. .data = &(const struct max20086_chip_info) {
  262. .id = MAX20086_DEVICE_ID_MAX20089,
  263. .num_outputs = 2,
  264. }
  265. },
  266. { /* Sentinel */ },
  267. };
  268. MODULE_DEVICE_TABLE(of, max20086_dt_ids);
  269. static struct i2c_driver max20086_regulator_driver = {
  270. .driver = {
  271. .name = "max20086",
  272. .of_match_table = of_match_ptr(max20086_dt_ids),
  273. },
  274. .probe_new = max20086_i2c_probe,
  275. .id_table = max20086_i2c_id,
  276. };
  277. module_i2c_driver(max20086_regulator_driver);
  278. MODULE_AUTHOR("Watson Chow <[email protected]>");
  279. MODULE_DESCRIPTION("MAX20086-MAX20089 Camera Power Protector Driver");
  280. MODULE_LICENSE("GPL");