Linux Audio

Check our new training course

Loading...
v5.9
  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>
 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>
 17
 18#define UNDERVOLTAGE_STICKY_BIT	BIT(16)
 19
 20struct 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;
 25};
 26
 27static void rpi_firmware_get_throttled(struct rpi_hwmon_data *data)
 28{
 29	u32 new_uv, old_uv, value;
 30	int ret;
 31
 32	/* Request firmware to clear sticky bits */
 33	value = 0xffff;
 34
 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;
 41	}
 42
 43	new_uv = value & UNDERVOLTAGE_STICKY_BIT;
 44	old_uv = data->last_throttled & UNDERVOLTAGE_STICKY_BIT;
 45	data->last_throttled = value;
 46
 47	if (new_uv == old_uv)
 48		return;
 49
 50	if (new_uv)
 51		dev_crit(data->hwmon_dev, "Undervoltage detected!\n");
 52	else
 53		dev_info(data->hwmon_dev, "Voltage normalised\n");
 54
 55	sysfs_notify(&data->hwmon_dev->kobj, NULL, "in0_lcrit_alarm");
 56}
 57
 58static void get_values_poll(struct work_struct *work)
 59{
 60	struct rpi_hwmon_data *data;
 61
 62	data = container_of(work, struct rpi_hwmon_data,
 63			    get_values_poll_work.work);
 64
 65	rpi_firmware_get_throttled(data);
 66
 67	/*
 68	 * We can't run faster than the sticky shift (100ms) since we get
 69	 * flipping in the sticky bits that are cleared.
 70	 */
 71	schedule_delayed_work(&data->get_values_poll_work, 2 * HZ);
 72}
 73
 74static int rpi_read(struct device *dev, enum hwmon_sensor_types type,
 75		    u32 attr, int channel, long *val)
 76{
 77	struct rpi_hwmon_data *data = dev_get_drvdata(dev);
 78
 79	*val = !!(data->last_throttled & UNDERVOLTAGE_STICKY_BIT);
 80	return 0;
 81}
 82
 83static umode_t rpi_is_visible(const void *_data, enum hwmon_sensor_types type,
 84			      u32 attr, int channel)
 85{
 86	return 0444;
 87}
 88
 89static const struct hwmon_channel_info *rpi_info[] = {
 90	HWMON_CHANNEL_INFO(in,
 91			   HWMON_I_LCRIT_ALARM),
 92	NULL
 93};
 94
 95static const struct hwmon_ops rpi_hwmon_ops = {
 96	.is_visible = rpi_is_visible,
 97	.read = rpi_read,
 98};
 99
100static const struct hwmon_chip_info rpi_chip_info = {
101	.ops = &rpi_hwmon_ops,
102	.info = rpi_info,
103};
104
105static int rpi_hwmon_probe(struct platform_device *pdev)
106{
107	struct device *dev = &pdev->dev;
108	struct rpi_hwmon_data *data;
109
110	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
111	if (!data)
112		return -ENOMEM;
113
114	/* Parent driver assure that firmware is correct */
115	data->fw = dev_get_drvdata(dev->parent);
116
117	data->hwmon_dev = devm_hwmon_device_register_with_info(dev, "rpi_volt",
118							       data,
119							       &rpi_chip_info,
120							       NULL);
121
122	INIT_DELAYED_WORK(&data->get_values_poll_work, get_values_poll);
123	platform_set_drvdata(pdev, data);
124
125	if (!PTR_ERR_OR_ZERO(data->hwmon_dev))
126		schedule_delayed_work(&data->get_values_poll_work, 2 * HZ);
127
128	return PTR_ERR_OR_ZERO(data->hwmon_dev);
129}
130
131static int rpi_hwmon_remove(struct platform_device *pdev)
132{
133	struct rpi_hwmon_data *data = platform_get_drvdata(pdev);
134
135	cancel_delayed_work_sync(&data->get_values_poll_work);
136
137	return 0;
138}
139
140static struct platform_driver rpi_hwmon_driver = {
141	.probe = rpi_hwmon_probe,
142	.remove = rpi_hwmon_remove,
143	.driver = {
144		.name = "raspberrypi-hwmon",
145	},
146};
147module_platform_driver(rpi_hwmon_driver);
148
149MODULE_AUTHOR("Stefan Wahren <wahrenst@gmx.net>");
150MODULE_DESCRIPTION("Raspberry Pi voltage sensor driver");
151MODULE_LICENSE("GPL v2");
152MODULE_ALIAS("platform:raspberrypi-hwmon");
v5.4
  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>
 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>
 17
 18#define UNDERVOLTAGE_STICKY_BIT	BIT(16)
 19
 20struct 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;
 25};
 26
 27static void rpi_firmware_get_throttled(struct rpi_hwmon_data *data)
 28{
 29	u32 new_uv, old_uv, value;
 30	int ret;
 31
 32	/* Request firmware to clear sticky bits */
 33	value = 0xffff;
 34
 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;
 41	}
 42
 43	new_uv = value & UNDERVOLTAGE_STICKY_BIT;
 44	old_uv = data->last_throttled & UNDERVOLTAGE_STICKY_BIT;
 45	data->last_throttled = value;
 46
 47	if (new_uv == old_uv)
 48		return;
 49
 50	if (new_uv)
 51		dev_crit(data->hwmon_dev, "Undervoltage detected!\n");
 52	else
 53		dev_info(data->hwmon_dev, "Voltage normalised\n");
 54
 55	sysfs_notify(&data->hwmon_dev->kobj, NULL, "in0_lcrit_alarm");
 56}
 57
 58static void get_values_poll(struct work_struct *work)
 59{
 60	struct rpi_hwmon_data *data;
 61
 62	data = container_of(work, struct rpi_hwmon_data,
 63			    get_values_poll_work.work);
 64
 65	rpi_firmware_get_throttled(data);
 66
 67	/*
 68	 * We can't run faster than the sticky shift (100ms) since we get
 69	 * flipping in the sticky bits that are cleared.
 70	 */
 71	schedule_delayed_work(&data->get_values_poll_work, 2 * HZ);
 72}
 73
 74static int rpi_read(struct device *dev, enum hwmon_sensor_types type,
 75		    u32 attr, int channel, long *val)
 76{
 77	struct rpi_hwmon_data *data = dev_get_drvdata(dev);
 78
 79	*val = !!(data->last_throttled & UNDERVOLTAGE_STICKY_BIT);
 80	return 0;
 81}
 82
 83static umode_t rpi_is_visible(const void *_data, enum hwmon_sensor_types type,
 84			      u32 attr, int channel)
 85{
 86	return 0444;
 87}
 88
 89static const struct hwmon_channel_info *rpi_info[] = {
 90	HWMON_CHANNEL_INFO(in,
 91			   HWMON_I_LCRIT_ALARM),
 92	NULL
 93};
 94
 95static const struct hwmon_ops rpi_hwmon_ops = {
 96	.is_visible = rpi_is_visible,
 97	.read = rpi_read,
 98};
 99
100static const struct hwmon_chip_info rpi_chip_info = {
101	.ops = &rpi_hwmon_ops,
102	.info = rpi_info,
103};
104
105static int rpi_hwmon_probe(struct platform_device *pdev)
106{
107	struct device *dev = &pdev->dev;
108	struct rpi_hwmon_data *data;
109
110	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
111	if (!data)
112		return -ENOMEM;
113
114	/* Parent driver assure that firmware is correct */
115	data->fw = dev_get_drvdata(dev->parent);
116
117	data->hwmon_dev = devm_hwmon_device_register_with_info(dev, "rpi_volt",
118							       data,
119							       &rpi_chip_info,
120							       NULL);
121
122	INIT_DELAYED_WORK(&data->get_values_poll_work, get_values_poll);
123	platform_set_drvdata(pdev, data);
124
125	if (!PTR_ERR_OR_ZERO(data->hwmon_dev))
126		schedule_delayed_work(&data->get_values_poll_work, 2 * HZ);
127
128	return PTR_ERR_OR_ZERO(data->hwmon_dev);
129}
130
131static int rpi_hwmon_remove(struct platform_device *pdev)
132{
133	struct rpi_hwmon_data *data = platform_get_drvdata(pdev);
134
135	cancel_delayed_work_sync(&data->get_values_poll_work);
136
137	return 0;
138}
139
140static struct platform_driver rpi_hwmon_driver = {
141	.probe = rpi_hwmon_probe,
142	.remove = rpi_hwmon_remove,
143	.driver = {
144		.name = "raspberrypi-hwmon",
145	},
146};
147module_platform_driver(rpi_hwmon_driver);
148
149MODULE_AUTHOR("Stefan Wahren <wahrenst@gmx.net>");
150MODULE_DESCRIPTION("Raspberry Pi voltage sensor driver");
151MODULE_LICENSE("GPL v2");
152MODULE_ALIAS("platform:raspberrypi-hwmon");