ene-kb3930.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0-or-later
  2. /*
  3. * ENE KB3930 Embedded Controller Driver
  4. *
  5. * Copyright (C) 2020 Lubomir Rintel
  6. */
  7. #include <linux/delay.h>
  8. #include <linux/gpio/consumer.h>
  9. #include <linux/i2c.h>
  10. #include <linux/mfd/core.h>
  11. #include <linux/module.h>
  12. #include <linux/reboot.h>
  13. #include <linux/regmap.h>
  14. /* I2C registers that are multiplexing access to the EC RAM. */
  15. enum {
  16. EC_DATA_IN = 0x00,
  17. EC_RAM_OUT = 0x80,
  18. EC_RAM_IN = 0x81,
  19. };
  20. /* EC RAM registers. */
  21. enum {
  22. EC_MODEL = 0x30,
  23. EC_VERSION_MAJ = 0x31,
  24. EC_VERSION_MIN = 0x32,
  25. };
  26. struct kb3930 {
  27. struct i2c_client *client;
  28. struct regmap *ram_regmap;
  29. struct gpio_descs *off_gpios;
  30. };
  31. struct kb3930 *kb3930_power_off;
  32. #define EC_GPIO_WAVE 0
  33. #define EC_GPIO_OFF_MODE 1
  34. #define EC_OFF_MODE_REBOOT 0
  35. #define EC_OFF_MODE_POWER 1
  36. static void kb3930_off(struct kb3930 *ddata, int off_mode)
  37. {
  38. gpiod_direction_output(ddata->off_gpios->desc[EC_GPIO_OFF_MODE],
  39. off_mode);
  40. /*
  41. * This creates a 10 Hz wave on EC_GPIO_WAVE that signals a
  42. * shutdown request to the EC. Once the EC detects it, it will
  43. * proceed to turn the power off or reset the board depending on
  44. * the value of EC_GPIO_OFF_MODE.
  45. */
  46. while (1) {
  47. mdelay(50);
  48. gpiod_direction_output(ddata->off_gpios->desc[EC_GPIO_WAVE], 0);
  49. mdelay(50);
  50. gpiod_direction_output(ddata->off_gpios->desc[EC_GPIO_WAVE], 1);
  51. }
  52. }
  53. static int kb3930_restart(struct notifier_block *this,
  54. unsigned long mode, void *cmd)
  55. {
  56. kb3930_off(kb3930_power_off, EC_OFF_MODE_REBOOT);
  57. return NOTIFY_DONE;
  58. }
  59. static void kb3930_pm_power_off(void)
  60. {
  61. kb3930_off(kb3930_power_off, EC_OFF_MODE_POWER);
  62. }
  63. static struct notifier_block kb3930_restart_nb = {
  64. .notifier_call = kb3930_restart,
  65. };
  66. static const struct mfd_cell ariel_ec_cells[] = {
  67. { .name = "dell-wyse-ariel-led", },
  68. { .name = "dell-wyse-ariel-power", },
  69. };
  70. static int kb3930_ec_ram_reg_write(void *context, unsigned int reg,
  71. unsigned int val)
  72. {
  73. struct kb3930 *ddata = context;
  74. return i2c_smbus_write_word_data(ddata->client, EC_RAM_OUT,
  75. (val << 8) | reg);
  76. }
  77. static int kb3930_ec_ram_reg_read(void *context, unsigned int reg,
  78. unsigned int *val)
  79. {
  80. struct kb3930 *ddata = context;
  81. int ret;
  82. ret = i2c_smbus_write_word_data(ddata->client, EC_RAM_IN, reg);
  83. if (ret < 0)
  84. return ret;
  85. ret = i2c_smbus_read_word_data(ddata->client, EC_DATA_IN);
  86. if (ret < 0)
  87. return ret;
  88. *val = ret >> 8;
  89. return 0;
  90. }
  91. static const struct regmap_config kb3930_ram_regmap_config = {
  92. .name = "ec_ram",
  93. .reg_bits = 8,
  94. .val_bits = 8,
  95. .reg_stride = 1,
  96. .max_register = 0xff,
  97. .reg_write = kb3930_ec_ram_reg_write,
  98. .reg_read = kb3930_ec_ram_reg_read,
  99. .fast_io = false,
  100. };
  101. static int kb3930_probe(struct i2c_client *client)
  102. {
  103. struct device *dev = &client->dev;
  104. struct device_node *np = dev->of_node;
  105. struct kb3930 *ddata;
  106. unsigned int model;
  107. int ret;
  108. ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
  109. if (!ddata)
  110. return -ENOMEM;
  111. kb3930_power_off = ddata;
  112. ddata->client = client;
  113. i2c_set_clientdata(client, ddata);
  114. ddata->ram_regmap = devm_regmap_init(dev, NULL, ddata,
  115. &kb3930_ram_regmap_config);
  116. if (IS_ERR(ddata->ram_regmap))
  117. return PTR_ERR(ddata->ram_regmap);
  118. ret = regmap_read(ddata->ram_regmap, EC_MODEL, &model);
  119. if (ret < 0)
  120. return ret;
  121. /* Currently we only support the cells present on Dell Ariel model. */
  122. if (model != 'J') {
  123. dev_err(dev, "unknown board model: %02x\n", model);
  124. return -ENODEV;
  125. }
  126. ret = devm_mfd_add_devices(dev, PLATFORM_DEVID_AUTO,
  127. ariel_ec_cells,
  128. ARRAY_SIZE(ariel_ec_cells),
  129. NULL, 0, NULL);
  130. if (ret)
  131. return ret;
  132. if (of_property_read_bool(np, "system-power-controller")) {
  133. ddata->off_gpios =
  134. devm_gpiod_get_array_optional(dev, "off", GPIOD_IN);
  135. if (IS_ERR(ddata->off_gpios))
  136. return PTR_ERR(ddata->off_gpios);
  137. if (ddata->off_gpios->ndescs < 2) {
  138. dev_err(dev, "invalid off-gpios property\n");
  139. return -EINVAL;
  140. }
  141. }
  142. if (ddata->off_gpios) {
  143. register_restart_handler(&kb3930_restart_nb);
  144. if (!pm_power_off)
  145. pm_power_off = kb3930_pm_power_off;
  146. }
  147. return 0;
  148. }
  149. static int kb3930_remove(struct i2c_client *client)
  150. {
  151. struct kb3930 *ddata = i2c_get_clientdata(client);
  152. if (ddata->off_gpios) {
  153. if (pm_power_off == kb3930_pm_power_off)
  154. pm_power_off = NULL;
  155. unregister_restart_handler(&kb3930_restart_nb);
  156. }
  157. kb3930_power_off = NULL;
  158. return 0;
  159. }
  160. static const struct of_device_id kb3930_dt_ids[] = {
  161. { .compatible = "ene,kb3930" },
  162. { }
  163. };
  164. MODULE_DEVICE_TABLE(of, kb3930_dt_ids);
  165. static struct i2c_driver kb3930_driver = {
  166. .probe_new = kb3930_probe,
  167. .remove = kb3930_remove,
  168. .driver = {
  169. .name = "ene-kb3930",
  170. .of_match_table = of_match_ptr(kb3930_dt_ids),
  171. },
  172. };
  173. module_i2c_driver(kb3930_driver);
  174. MODULE_AUTHOR("Lubomir Rintel <lkundrak@v3.sk>");
  175. MODULE_DESCRIPTION("ENE KB3930 Embedded Controller Driver");
  176. MODULE_LICENSE("Dual BSD/GPL");