Stefan Wahren | 74d1e00 | 2018-05-25 21:24:35 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Raspberry Pi voltage sensor driver |
| 4 | * |
| 5 | * Based on firmware/raspberrypi.c by Noralf Trønnes |
| 6 | * |
| 7 | * Copyright (C) 2018 Stefan Wahren <stefan.wahren@i2se.com> |
| 8 | */ |
| 9 | #include <linux/device.h> |
Matti Vaittinen | 96dc2b3 | 2021-03-23 15:57:25 +0200 | [diff] [blame] | 10 | #include <linux/devm-helpers.h> |
Stefan Wahren | 74d1e00 | 2018-05-25 21:24:35 +0200 | [diff] [blame] | 11 | #include <linux/err.h> |
| 12 | #include <linux/hwmon.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/platform_device.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/workqueue.h> |
| 17 | #include <soc/bcm2835/raspberrypi-firmware.h> |
| 18 | |
| 19 | #define UNDERVOLTAGE_STICKY_BIT BIT(16) |
| 20 | |
| 21 | struct rpi_hwmon_data { |
| 22 | struct device *hwmon_dev; |
| 23 | struct rpi_firmware *fw; |
| 24 | u32 last_throttled; |
| 25 | struct delayed_work get_values_poll_work; |
| 26 | }; |
| 27 | |
| 28 | static void rpi_firmware_get_throttled(struct rpi_hwmon_data *data) |
| 29 | { |
| 30 | u32 new_uv, old_uv, value; |
| 31 | int ret; |
| 32 | |
| 33 | /* Request firmware to clear sticky bits */ |
| 34 | value = 0xffff; |
| 35 | |
| 36 | ret = rpi_firmware_property(data->fw, RPI_FIRMWARE_GET_THROTTLED, |
| 37 | &value, sizeof(value)); |
| 38 | if (ret) { |
| 39 | dev_err_once(data->hwmon_dev, "Failed to get throttled (%d)\n", |
| 40 | ret); |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | new_uv = value & UNDERVOLTAGE_STICKY_BIT; |
| 45 | old_uv = data->last_throttled & UNDERVOLTAGE_STICKY_BIT; |
| 46 | data->last_throttled = value; |
| 47 | |
| 48 | if (new_uv == old_uv) |
| 49 | return; |
| 50 | |
| 51 | if (new_uv) |
| 52 | dev_crit(data->hwmon_dev, "Undervoltage detected!\n"); |
| 53 | else |
| 54 | dev_info(data->hwmon_dev, "Voltage normalised\n"); |
| 55 | |
Armin Wolf | d73287e | 2021-09-05 21:00:49 +0200 | [diff] [blame] | 56 | hwmon_notify_event(data->hwmon_dev, hwmon_in, hwmon_in_lcrit_alarm, 0); |
Stefan Wahren | 74d1e00 | 2018-05-25 21:24:35 +0200 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | static void get_values_poll(struct work_struct *work) |
| 60 | { |
| 61 | struct rpi_hwmon_data *data; |
| 62 | |
| 63 | data = container_of(work, struct rpi_hwmon_data, |
| 64 | get_values_poll_work.work); |
| 65 | |
| 66 | rpi_firmware_get_throttled(data); |
| 67 | |
| 68 | /* |
| 69 | * We can't run faster than the sticky shift (100ms) since we get |
| 70 | * flipping in the sticky bits that are cleared. |
| 71 | */ |
| 72 | schedule_delayed_work(&data->get_values_poll_work, 2 * HZ); |
| 73 | } |
| 74 | |
| 75 | static int rpi_read(struct device *dev, enum hwmon_sensor_types type, |
| 76 | u32 attr, int channel, long *val) |
| 77 | { |
| 78 | struct rpi_hwmon_data *data = dev_get_drvdata(dev); |
| 79 | |
| 80 | *val = !!(data->last_throttled & UNDERVOLTAGE_STICKY_BIT); |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | static umode_t rpi_is_visible(const void *_data, enum hwmon_sensor_types type, |
| 85 | u32 attr, int channel) |
| 86 | { |
| 87 | return 0444; |
| 88 | } |
| 89 | |
Stefan Wahren | 74d1e00 | 2018-05-25 21:24:35 +0200 | [diff] [blame] | 90 | static const struct hwmon_channel_info *rpi_info[] = { |
Guenter Roeck | 0c42186 | 2019-03-31 10:53:50 -0700 | [diff] [blame] | 91 | HWMON_CHANNEL_INFO(in, |
| 92 | HWMON_I_LCRIT_ALARM), |
Stefan Wahren | 74d1e00 | 2018-05-25 21:24:35 +0200 | [diff] [blame] | 93 | NULL |
| 94 | }; |
| 95 | |
| 96 | static const struct hwmon_ops rpi_hwmon_ops = { |
| 97 | .is_visible = rpi_is_visible, |
| 98 | .read = rpi_read, |
| 99 | }; |
| 100 | |
| 101 | static const struct hwmon_chip_info rpi_chip_info = { |
| 102 | .ops = &rpi_hwmon_ops, |
| 103 | .info = rpi_info, |
| 104 | }; |
| 105 | |
| 106 | static int rpi_hwmon_probe(struct platform_device *pdev) |
| 107 | { |
| 108 | struct device *dev = &pdev->dev; |
| 109 | struct rpi_hwmon_data *data; |
Matti Vaittinen | 96dc2b3 | 2021-03-23 15:57:25 +0200 | [diff] [blame] | 110 | int ret; |
Stefan Wahren | 74d1e00 | 2018-05-25 21:24:35 +0200 | [diff] [blame] | 111 | |
| 112 | data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); |
| 113 | if (!data) |
| 114 | return -ENOMEM; |
| 115 | |
| 116 | /* Parent driver assure that firmware is correct */ |
| 117 | data->fw = dev_get_drvdata(dev->parent); |
| 118 | |
Stefan Wahren | 74d1e00 | 2018-05-25 21:24:35 +0200 | [diff] [blame] | 119 | data->hwmon_dev = devm_hwmon_device_register_with_info(dev, "rpi_volt", |
| 120 | data, |
| 121 | &rpi_chip_info, |
| 122 | NULL); |
Armin Wolf | 9c6d555 | 2021-12-11 19:44:49 +0100 | [diff] [blame] | 123 | if (IS_ERR(data->hwmon_dev)) |
| 124 | return PTR_ERR(data->hwmon_dev); |
Stefan Wahren | 74d1e00 | 2018-05-25 21:24:35 +0200 | [diff] [blame] | 125 | |
Matti Vaittinen | 96dc2b3 | 2021-03-23 15:57:25 +0200 | [diff] [blame] | 126 | ret = devm_delayed_work_autocancel(dev, &data->get_values_poll_work, |
| 127 | get_values_poll); |
| 128 | if (ret) |
| 129 | return ret; |
Stefan Wahren | 74d1e00 | 2018-05-25 21:24:35 +0200 | [diff] [blame] | 130 | platform_set_drvdata(pdev, data); |
| 131 | |
Armin Wolf | 9c6d555 | 2021-12-11 19:44:49 +0100 | [diff] [blame] | 132 | schedule_delayed_work(&data->get_values_poll_work, 2 * HZ); |
Stefan Wahren | 74d1e00 | 2018-05-25 21:24:35 +0200 | [diff] [blame] | 133 | |
Armin Wolf | 9c6d555 | 2021-12-11 19:44:49 +0100 | [diff] [blame] | 134 | return 0; |
Stefan Wahren | 74d1e00 | 2018-05-25 21:24:35 +0200 | [diff] [blame] | 135 | } |
| 136 | |
Stefan Wahren | 74d1e00 | 2018-05-25 21:24:35 +0200 | [diff] [blame] | 137 | static struct platform_driver rpi_hwmon_driver = { |
| 138 | .probe = rpi_hwmon_probe, |
Stefan Wahren | 74d1e00 | 2018-05-25 21:24:35 +0200 | [diff] [blame] | 139 | .driver = { |
| 140 | .name = "raspberrypi-hwmon", |
| 141 | }, |
| 142 | }; |
| 143 | module_platform_driver(rpi_hwmon_driver); |
| 144 | |
Stefan Wahren | 10b1f2a | 2019-08-13 20:17:28 +0200 | [diff] [blame] | 145 | MODULE_AUTHOR("Stefan Wahren <wahrenst@gmx.net>"); |
Stefan Wahren | 74d1e00 | 2018-05-25 21:24:35 +0200 | [diff] [blame] | 146 | MODULE_DESCRIPTION("Raspberry Pi voltage sensor driver"); |
| 147 | MODULE_LICENSE("GPL v2"); |
Peter Robinson | d07f05f | 2018-07-21 00:02:12 +0100 | [diff] [blame] | 148 | MODULE_ALIAS("platform:raspberrypi-hwmon"); |