rpi-panel-v2-regulator.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2022 Raspberry Pi Ltd.
  4. * Copyright (C) 2025 Marek Vasut
  5. */
  6. #include <linux/err.h>
  7. #include <linux/gpio/driver.h>
  8. #include <linux/gpio/regmap.h>
  9. #include <linux/i2c.h>
  10. #include <linux/module.h>
  11. #include <linux/pwm.h>
  12. #include <linux/regmap.h>
  13. /* I2C registers of the microcontroller. */
  14. #define REG_ID 0x01
  15. #define REG_POWERON 0x02
  16. #define REG_PWM 0x03
  17. /* Bits for poweron register */
  18. #define LCD_RESET_BIT BIT(0)
  19. #define CTP_RESET_BIT BIT(1)
  20. /* Bits for the PWM register */
  21. #define PWM_BL_ENABLE BIT(7)
  22. #define PWM_BL_MASK GENMASK(4, 0)
  23. /* Treat LCD_RESET and CTP_RESET as GPIOs */
  24. #define NUM_GPIO 2
  25. static const struct regmap_config rpi_panel_regmap_config = {
  26. .reg_bits = 8,
  27. .val_bits = 8,
  28. .max_register = REG_PWM,
  29. .can_sleep = true,
  30. };
  31. static int rpi_panel_v2_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  32. const struct pwm_state *state)
  33. {
  34. struct regmap *regmap = pwmchip_get_drvdata(chip);
  35. unsigned int duty;
  36. if (state->polarity != PWM_POLARITY_NORMAL)
  37. return -EINVAL;
  38. if (!state->enabled)
  39. return regmap_write(regmap, REG_PWM, 0);
  40. duty = pwm_get_relative_duty_cycle(state, PWM_BL_MASK);
  41. return regmap_write(regmap, REG_PWM, duty | PWM_BL_ENABLE);
  42. }
  43. static const struct pwm_ops rpi_panel_v2_pwm_ops = {
  44. .apply = rpi_panel_v2_pwm_apply,
  45. };
  46. /*
  47. * I2C driver interface functions
  48. */
  49. static int rpi_panel_v2_i2c_probe(struct i2c_client *i2c)
  50. {
  51. struct gpio_regmap_config gconfig = {
  52. .ngpio = NUM_GPIO,
  53. .ngpio_per_reg = NUM_GPIO,
  54. .parent = &i2c->dev,
  55. .reg_set_base = REG_POWERON,
  56. };
  57. struct regmap *regmap;
  58. struct pwm_chip *pc;
  59. int ret;
  60. pc = devm_pwmchip_alloc(&i2c->dev, 1, 0);
  61. if (IS_ERR(pc))
  62. return PTR_ERR(pc);
  63. pc->ops = &rpi_panel_v2_pwm_ops;
  64. regmap = devm_regmap_init_i2c(i2c, &rpi_panel_regmap_config);
  65. if (IS_ERR(regmap))
  66. return dev_err_probe(&i2c->dev, PTR_ERR(regmap), "Failed to allocate regmap\n");
  67. pwmchip_set_drvdata(pc, regmap);
  68. regmap_write(regmap, REG_POWERON, 0);
  69. gconfig.regmap = regmap;
  70. ret = PTR_ERR_OR_ZERO(devm_gpio_regmap_register(&i2c->dev, &gconfig));
  71. if (ret)
  72. return dev_err_probe(&i2c->dev, ret, "Failed to create gpiochip\n");
  73. i2c_set_clientdata(i2c, regmap);
  74. return devm_pwmchip_add(&i2c->dev, pc);
  75. }
  76. static void rpi_panel_v2_i2c_shutdown(struct i2c_client *client)
  77. {
  78. struct regmap *regmap = i2c_get_clientdata(client);
  79. regmap_write(regmap, REG_PWM, 0);
  80. regmap_write(regmap, REG_POWERON, 0);
  81. }
  82. static const struct of_device_id rpi_panel_v2_dt_ids[] = {
  83. { .compatible = "raspberrypi,touchscreen-panel-regulator-v2" },
  84. { },
  85. };
  86. MODULE_DEVICE_TABLE(of, rpi_panel_v2_dt_ids);
  87. static struct i2c_driver rpi_panel_v2_regulator_driver = {
  88. .driver = {
  89. .name = "rpi_touchscreen_v2",
  90. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  91. .of_match_table = rpi_panel_v2_dt_ids,
  92. },
  93. .probe = rpi_panel_v2_i2c_probe,
  94. .shutdown = rpi_panel_v2_i2c_shutdown,
  95. };
  96. module_i2c_driver(rpi_panel_v2_regulator_driver);
  97. MODULE_AUTHOR("Dave Stevenson <dave.stevenson@raspberrypi.com>");
  98. MODULE_DESCRIPTION("Regulator device driver for Raspberry Pi 7-inch V2 touchscreen");
  99. MODULE_LICENSE("GPL");