perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / drivers / hwmon / raspberrypi-hwmon.c
blobbe5ba469089531b26b0ba2d0b1866c20f3e5312b
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Raspberry Pi voltage sensor driver
5 * Based on firmware/raspberrypi.c by Noralf Trønnes
7 * Copyright (C) 2018 Stefan Wahren <stefan.wahren@i2se.com>
8 */
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/hwmon.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.h>
15 #include <linux/workqueue.h>
16 #include <soc/bcm2835/raspberrypi-firmware.h>
18 #define UNDERVOLTAGE_STICKY_BIT BIT(16)
20 struct rpi_hwmon_data {
21 struct device *hwmon_dev;
22 struct rpi_firmware *fw;
23 u32 last_throttled;
24 struct delayed_work get_values_poll_work;
27 static void rpi_firmware_get_throttled(struct rpi_hwmon_data *data)
29 u32 new_uv, old_uv, value;
30 int ret;
32 /* Request firmware to clear sticky bits */
33 value = 0xffff;
35 ret = rpi_firmware_property(data->fw, RPI_FIRMWARE_GET_THROTTLED,
36 &value, sizeof(value));
37 if (ret) {
38 dev_err_once(data->hwmon_dev, "Failed to get throttled (%d)\n",
39 ret);
40 return;
43 new_uv = value & UNDERVOLTAGE_STICKY_BIT;
44 old_uv = data->last_throttled & UNDERVOLTAGE_STICKY_BIT;
45 data->last_throttled = value;
47 if (new_uv == old_uv)
48 return;
50 if (new_uv)
51 dev_crit(data->hwmon_dev, "Undervoltage detected!\n");
52 else
53 dev_info(data->hwmon_dev, "Voltage normalised\n");
55 sysfs_notify(&data->hwmon_dev->kobj, NULL, "in0_lcrit_alarm");
58 static void get_values_poll(struct work_struct *work)
60 struct rpi_hwmon_data *data;
62 data = container_of(work, struct rpi_hwmon_data,
63 get_values_poll_work.work);
65 rpi_firmware_get_throttled(data);
68 * We can't run faster than the sticky shift (100ms) since we get
69 * flipping in the sticky bits that are cleared.
71 schedule_delayed_work(&data->get_values_poll_work, 2 * HZ);
74 static int rpi_read(struct device *dev, enum hwmon_sensor_types type,
75 u32 attr, int channel, long *val)
77 struct rpi_hwmon_data *data = dev_get_drvdata(dev);
79 *val = !!(data->last_throttled & UNDERVOLTAGE_STICKY_BIT);
80 return 0;
83 static umode_t rpi_is_visible(const void *_data, enum hwmon_sensor_types type,
84 u32 attr, int channel)
86 return 0444;
89 static const u32 rpi_in_config[] = {
90 HWMON_I_LCRIT_ALARM,
94 static const struct hwmon_channel_info rpi_in = {
95 .type = hwmon_in,
96 .config = rpi_in_config,
99 static const struct hwmon_channel_info *rpi_info[] = {
100 &rpi_in,
101 NULL
104 static const struct hwmon_ops rpi_hwmon_ops = {
105 .is_visible = rpi_is_visible,
106 .read = rpi_read,
109 static const struct hwmon_chip_info rpi_chip_info = {
110 .ops = &rpi_hwmon_ops,
111 .info = rpi_info,
114 static int rpi_hwmon_probe(struct platform_device *pdev)
116 struct device *dev = &pdev->dev;
117 struct rpi_hwmon_data *data;
118 int ret;
120 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
121 if (!data)
122 return -ENOMEM;
124 /* Parent driver assure that firmware is correct */
125 data->fw = dev_get_drvdata(dev->parent);
127 /* Init throttled */
128 ret = rpi_firmware_property(data->fw, RPI_FIRMWARE_GET_THROTTLED,
129 &data->last_throttled,
130 sizeof(data->last_throttled));
132 data->hwmon_dev = devm_hwmon_device_register_with_info(dev, "rpi_volt",
133 data,
134 &rpi_chip_info,
135 NULL);
137 INIT_DELAYED_WORK(&data->get_values_poll_work, get_values_poll);
138 platform_set_drvdata(pdev, data);
140 if (!PTR_ERR_OR_ZERO(data->hwmon_dev))
141 schedule_delayed_work(&data->get_values_poll_work, 2 * HZ);
143 return PTR_ERR_OR_ZERO(data->hwmon_dev);
146 static int rpi_hwmon_remove(struct platform_device *pdev)
148 struct rpi_hwmon_data *data = platform_get_drvdata(pdev);
150 cancel_delayed_work_sync(&data->get_values_poll_work);
152 return 0;
155 static struct platform_driver rpi_hwmon_driver = {
156 .probe = rpi_hwmon_probe,
157 .remove = rpi_hwmon_remove,
158 .driver = {
159 .name = "raspberrypi-hwmon",
162 module_platform_driver(rpi_hwmon_driver);
164 MODULE_AUTHOR("Stefan Wahren <stefan.wahren@i2se.com>");
165 MODULE_DESCRIPTION("Raspberry Pi voltage sensor driver");
166 MODULE_LICENSE("GPL v2");
167 MODULE_ALIAS("platform:raspberrypi-hwmon");