lp87565.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2017 Texas Instruments Incorporated - https://www.ti.com/
  4. *
  5. * Author: Keerthy <[email protected]>
  6. */
  7. #include <linux/gpio/consumer.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/mfd/core.h>
  10. #include <linux/module.h>
  11. #include <linux/of_device.h>
  12. #include <linux/regmap.h>
  13. #include <linux/mfd/lp87565.h>
  14. static const struct regmap_config lp87565_regmap_config = {
  15. .reg_bits = 8,
  16. .val_bits = 8,
  17. .max_register = LP87565_REG_MAX,
  18. };
  19. static const struct mfd_cell lp87565_cells[] = {
  20. { .name = "lp87565-q1-regulator", },
  21. { .name = "lp87565-q1-gpio", },
  22. };
  23. static const struct of_device_id of_lp87565_match_table[] = {
  24. { .compatible = "ti,lp87565", },
  25. {
  26. .compatible = "ti,lp87524-q1",
  27. .data = (void *)LP87565_DEVICE_TYPE_LP87524_Q1,
  28. },
  29. {
  30. .compatible = "ti,lp87565-q1",
  31. .data = (void *)LP87565_DEVICE_TYPE_LP87565_Q1,
  32. },
  33. {
  34. .compatible = "ti,lp87561-q1",
  35. .data = (void *)LP87565_DEVICE_TYPE_LP87561_Q1,
  36. },
  37. {}
  38. };
  39. MODULE_DEVICE_TABLE(of, of_lp87565_match_table);
  40. static int lp87565_probe(struct i2c_client *client,
  41. const struct i2c_device_id *ids)
  42. {
  43. struct lp87565 *lp87565;
  44. const struct of_device_id *of_id;
  45. int ret;
  46. unsigned int otpid;
  47. lp87565 = devm_kzalloc(&client->dev, sizeof(*lp87565), GFP_KERNEL);
  48. if (!lp87565)
  49. return -ENOMEM;
  50. lp87565->dev = &client->dev;
  51. lp87565->regmap = devm_regmap_init_i2c(client, &lp87565_regmap_config);
  52. if (IS_ERR(lp87565->regmap)) {
  53. ret = PTR_ERR(lp87565->regmap);
  54. dev_err(lp87565->dev,
  55. "Failed to initialize register map: %d\n", ret);
  56. return ret;
  57. }
  58. lp87565->reset_gpio = devm_gpiod_get_optional(lp87565->dev, "reset",
  59. GPIOD_OUT_LOW);
  60. if (IS_ERR(lp87565->reset_gpio)) {
  61. ret = PTR_ERR(lp87565->reset_gpio);
  62. if (ret == -EPROBE_DEFER)
  63. return ret;
  64. }
  65. if (lp87565->reset_gpio) {
  66. gpiod_set_value_cansleep(lp87565->reset_gpio, 1);
  67. /* The minimum assertion time is undocumented, just guess */
  68. usleep_range(2000, 4000);
  69. gpiod_set_value_cansleep(lp87565->reset_gpio, 0);
  70. /* Min 1.2 ms before first I2C transaction */
  71. usleep_range(1500, 3000);
  72. }
  73. ret = regmap_read(lp87565->regmap, LP87565_REG_OTP_REV, &otpid);
  74. if (ret) {
  75. dev_err(lp87565->dev, "Failed to read OTP ID\n");
  76. return ret;
  77. }
  78. lp87565->rev = otpid & LP87565_OTP_REV_OTP_ID;
  79. of_id = of_match_device(of_lp87565_match_table, &client->dev);
  80. if (of_id)
  81. lp87565->dev_type = (enum lp87565_device_type)of_id->data;
  82. i2c_set_clientdata(client, lp87565);
  83. return devm_mfd_add_devices(lp87565->dev, PLATFORM_DEVID_AUTO,
  84. lp87565_cells, ARRAY_SIZE(lp87565_cells),
  85. NULL, 0, NULL);
  86. }
  87. static void lp87565_shutdown(struct i2c_client *client)
  88. {
  89. struct lp87565 *lp87565 = i2c_get_clientdata(client);
  90. gpiod_set_value_cansleep(lp87565->reset_gpio, 1);
  91. }
  92. static const struct i2c_device_id lp87565_id_table[] = {
  93. { "lp87565-q1", 0 },
  94. { },
  95. };
  96. MODULE_DEVICE_TABLE(i2c, lp87565_id_table);
  97. static struct i2c_driver lp87565_driver = {
  98. .driver = {
  99. .name = "lp87565",
  100. .of_match_table = of_lp87565_match_table,
  101. },
  102. .probe = lp87565_probe,
  103. .shutdown = lp87565_shutdown,
  104. .id_table = lp87565_id_table,
  105. };
  106. module_i2c_driver(lp87565_driver);
  107. MODULE_AUTHOR("J Keerthy <[email protected]>");
  108. MODULE_DESCRIPTION("lp87565 chip family Multi-Function Device driver");
  109. MODULE_LICENSE("GPL v2");