sel3350-platform.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // SPDX-License-Identifier: GPL-2.0-only OR BSD-3-Clause
  2. /*
  3. * Copyright 2023 Schweitzer Engineering Laboratories, Inc.
  4. * 2350 NE Hopkins Court, Pullman, WA 99163 USA
  5. *
  6. * Platform support for the b2093 mainboard used in SEL-3350 computers.
  7. * Consumes GPIO from the SoC to provide standard LED and power supply
  8. * devices.
  9. */
  10. #include <linux/acpi.h>
  11. #include <linux/gpio/consumer.h>
  12. #include <linux/gpio/machine.h>
  13. #include <linux/leds.h>
  14. #include <linux/module.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/power_supply.h>
  17. /* Broxton communities */
  18. #define BXT_NW "INT3452:01"
  19. #define BXT_W "INT3452:02"
  20. #define BXT_SW "INT3452:03"
  21. #define B2093_GPIO_ACPI_ID "SEL0003"
  22. #define SEL_PS_A "sel_ps_a"
  23. #define SEL_PS_A_DETECT "sel_ps_a_detect"
  24. #define SEL_PS_A_GOOD "sel_ps_a_good"
  25. #define SEL_PS_B "sel_ps_b"
  26. #define SEL_PS_B_DETECT "sel_ps_b_detect"
  27. #define SEL_PS_B_GOOD "sel_ps_b_good"
  28. /* LEDs */
  29. static const struct gpio_led sel3350_leds[] = {
  30. { .name = "sel:green:aux1" },
  31. { .name = "sel:green:aux2" },
  32. { .name = "sel:green:aux3" },
  33. { .name = "sel:green:aux4" },
  34. { .name = "sel:red:alarm" },
  35. { .name = "sel:green:enabled",
  36. .default_state = LEDS_GPIO_DEFSTATE_ON },
  37. { .name = "sel:red:aux1" },
  38. { .name = "sel:red:aux2" },
  39. { .name = "sel:red:aux3" },
  40. { .name = "sel:red:aux4" },
  41. };
  42. static const struct gpio_led_platform_data sel3350_leds_pdata = {
  43. .num_leds = ARRAY_SIZE(sel3350_leds),
  44. .leds = sel3350_leds,
  45. };
  46. /* Map GPIOs to LEDs */
  47. static struct gpiod_lookup_table sel3350_leds_table = {
  48. .dev_id = "leds-gpio",
  49. .table = {
  50. GPIO_LOOKUP_IDX(BXT_NW, 49, NULL, 0, GPIO_ACTIVE_HIGH),
  51. GPIO_LOOKUP_IDX(BXT_NW, 50, NULL, 1, GPIO_ACTIVE_HIGH),
  52. GPIO_LOOKUP_IDX(BXT_NW, 51, NULL, 2, GPIO_ACTIVE_HIGH),
  53. GPIO_LOOKUP_IDX(BXT_NW, 52, NULL, 3, GPIO_ACTIVE_HIGH),
  54. GPIO_LOOKUP_IDX(BXT_W, 20, NULL, 4, GPIO_ACTIVE_HIGH),
  55. GPIO_LOOKUP_IDX(BXT_W, 21, NULL, 5, GPIO_ACTIVE_HIGH),
  56. GPIO_LOOKUP_IDX(BXT_SW, 37, NULL, 6, GPIO_ACTIVE_HIGH),
  57. GPIO_LOOKUP_IDX(BXT_SW, 38, NULL, 7, GPIO_ACTIVE_HIGH),
  58. GPIO_LOOKUP_IDX(BXT_SW, 39, NULL, 8, GPIO_ACTIVE_HIGH),
  59. GPIO_LOOKUP_IDX(BXT_SW, 40, NULL, 9, GPIO_ACTIVE_HIGH),
  60. {},
  61. }
  62. };
  63. /* Map GPIOs to power supplies */
  64. static struct gpiod_lookup_table sel3350_gpios_table = {
  65. .dev_id = B2093_GPIO_ACPI_ID ":00",
  66. .table = {
  67. GPIO_LOOKUP(BXT_NW, 44, SEL_PS_A_DETECT, GPIO_ACTIVE_LOW),
  68. GPIO_LOOKUP(BXT_NW, 45, SEL_PS_A_GOOD, GPIO_ACTIVE_LOW),
  69. GPIO_LOOKUP(BXT_NW, 46, SEL_PS_B_DETECT, GPIO_ACTIVE_LOW),
  70. GPIO_LOOKUP(BXT_NW, 47, SEL_PS_B_GOOD, GPIO_ACTIVE_LOW),
  71. {},
  72. }
  73. };
  74. /* Power Supplies */
  75. struct sel3350_power_cfg_data {
  76. struct gpio_desc *ps_detect;
  77. struct gpio_desc *ps_good;
  78. };
  79. static int sel3350_power_get_property(struct power_supply *psy,
  80. enum power_supply_property psp,
  81. union power_supply_propval *val)
  82. {
  83. struct sel3350_power_cfg_data *data = power_supply_get_drvdata(psy);
  84. switch (psp) {
  85. case POWER_SUPPLY_PROP_HEALTH:
  86. if (gpiod_get_value(data->ps_detect)) {
  87. if (gpiod_get_value(data->ps_good))
  88. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  89. else
  90. val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
  91. } else {
  92. val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
  93. }
  94. break;
  95. case POWER_SUPPLY_PROP_PRESENT:
  96. val->intval = gpiod_get_value(data->ps_detect);
  97. break;
  98. case POWER_SUPPLY_PROP_ONLINE:
  99. val->intval = gpiod_get_value(data->ps_good);
  100. break;
  101. default:
  102. return -EINVAL;
  103. }
  104. return 0;
  105. }
  106. static const enum power_supply_property sel3350_power_properties[] = {
  107. POWER_SUPPLY_PROP_HEALTH,
  108. POWER_SUPPLY_PROP_PRESENT,
  109. POWER_SUPPLY_PROP_ONLINE,
  110. };
  111. static const struct power_supply_desc sel3350_ps_a_desc = {
  112. .name = SEL_PS_A,
  113. .type = POWER_SUPPLY_TYPE_MAINS,
  114. .properties = sel3350_power_properties,
  115. .num_properties = ARRAY_SIZE(sel3350_power_properties),
  116. .get_property = sel3350_power_get_property,
  117. };
  118. static const struct power_supply_desc sel3350_ps_b_desc = {
  119. .name = SEL_PS_B,
  120. .type = POWER_SUPPLY_TYPE_MAINS,
  121. .properties = sel3350_power_properties,
  122. .num_properties = ARRAY_SIZE(sel3350_power_properties),
  123. .get_property = sel3350_power_get_property,
  124. };
  125. struct sel3350_data {
  126. struct platform_device *leds_pdev;
  127. struct power_supply *ps_a;
  128. struct power_supply *ps_b;
  129. struct sel3350_power_cfg_data ps_a_cfg_data;
  130. struct sel3350_power_cfg_data ps_b_cfg_data;
  131. };
  132. static int sel3350_probe(struct platform_device *pdev)
  133. {
  134. int rs;
  135. struct sel3350_data *sel3350;
  136. struct power_supply_config ps_cfg = {};
  137. sel3350 = devm_kzalloc(&pdev->dev, sizeof(struct sel3350_data), GFP_KERNEL);
  138. if (!sel3350)
  139. return -ENOMEM;
  140. platform_set_drvdata(pdev, sel3350);
  141. gpiod_add_lookup_table(&sel3350_leds_table);
  142. gpiod_add_lookup_table(&sel3350_gpios_table);
  143. sel3350->leds_pdev = platform_device_register_data(
  144. NULL,
  145. "leds-gpio",
  146. PLATFORM_DEVID_NONE,
  147. &sel3350_leds_pdata,
  148. sizeof(sel3350_leds_pdata));
  149. if (IS_ERR(sel3350->leds_pdev)) {
  150. rs = PTR_ERR(sel3350->leds_pdev);
  151. dev_err(&pdev->dev, "Failed registering platform device: %d\n", rs);
  152. goto err_platform;
  153. }
  154. /* Power Supply A */
  155. sel3350->ps_a_cfg_data.ps_detect = devm_gpiod_get(&pdev->dev,
  156. SEL_PS_A_DETECT,
  157. GPIOD_IN);
  158. sel3350->ps_a_cfg_data.ps_good = devm_gpiod_get(&pdev->dev,
  159. SEL_PS_A_GOOD,
  160. GPIOD_IN);
  161. ps_cfg.drv_data = &sel3350->ps_a_cfg_data;
  162. sel3350->ps_a = devm_power_supply_register(&pdev->dev,
  163. &sel3350_ps_a_desc,
  164. &ps_cfg);
  165. if (IS_ERR(sel3350->ps_a)) {
  166. rs = PTR_ERR(sel3350->ps_a);
  167. dev_err(&pdev->dev, "Failed registering power supply A: %d\n", rs);
  168. goto err_ps;
  169. }
  170. /* Power Supply B */
  171. sel3350->ps_b_cfg_data.ps_detect = devm_gpiod_get(&pdev->dev,
  172. SEL_PS_B_DETECT,
  173. GPIOD_IN);
  174. sel3350->ps_b_cfg_data.ps_good = devm_gpiod_get(&pdev->dev,
  175. SEL_PS_B_GOOD,
  176. GPIOD_IN);
  177. ps_cfg.drv_data = &sel3350->ps_b_cfg_data;
  178. sel3350->ps_b = devm_power_supply_register(&pdev->dev,
  179. &sel3350_ps_b_desc,
  180. &ps_cfg);
  181. if (IS_ERR(sel3350->ps_b)) {
  182. rs = PTR_ERR(sel3350->ps_b);
  183. dev_err(&pdev->dev, "Failed registering power supply B: %d\n", rs);
  184. goto err_ps;
  185. }
  186. return 0;
  187. err_ps:
  188. platform_device_unregister(sel3350->leds_pdev);
  189. err_platform:
  190. gpiod_remove_lookup_table(&sel3350_gpios_table);
  191. gpiod_remove_lookup_table(&sel3350_leds_table);
  192. return rs;
  193. }
  194. static void sel3350_remove(struct platform_device *pdev)
  195. {
  196. struct sel3350_data *sel3350 = platform_get_drvdata(pdev);
  197. platform_device_unregister(sel3350->leds_pdev);
  198. gpiod_remove_lookup_table(&sel3350_gpios_table);
  199. gpiod_remove_lookup_table(&sel3350_leds_table);
  200. }
  201. static const struct acpi_device_id sel3350_device_ids[] = {
  202. { B2093_GPIO_ACPI_ID, 0 },
  203. { "", 0 },
  204. };
  205. MODULE_DEVICE_TABLE(acpi, sel3350_device_ids);
  206. static struct platform_driver sel3350_platform_driver = {
  207. .probe = sel3350_probe,
  208. .remove = sel3350_remove,
  209. .driver = {
  210. .name = "sel3350-platform",
  211. .acpi_match_table = sel3350_device_ids,
  212. },
  213. };
  214. module_platform_driver(sel3350_platform_driver);
  215. MODULE_AUTHOR("Schweitzer Engineering Laboratories");
  216. MODULE_DESCRIPTION("SEL-3350 platform driver");
  217. MODULE_LICENSE("Dual BSD/GPL");
  218. MODULE_SOFTDEP("pre: pinctrl_broxton leds-gpio");