| lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * drivers/hwmon/wm831x-hwmon.c - Wolfson Microelectronics WM831x PMIC | 
 | 3 |  *                                hardware monitoring features. | 
 | 4 |  * | 
 | 5 |  * Copyright (C) 2009 Wolfson Microelectronics plc | 
 | 6 |  * | 
 | 7 |  * This program is free software; you can redistribute it and/or modify it | 
 | 8 |  * under the terms of the GNU General Public License v2 as published by the | 
 | 9 |  * Free Software Foundation. | 
 | 10 |  * | 
 | 11 |  * This program is distributed in the hope that it will be useful, but WITHOUT | 
 | 12 |  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | 
 | 13 |  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for | 
 | 14 |  * more details. | 
 | 15 |  * | 
 | 16 |  * You should have received a copy of the GNU General Public License along with | 
 | 17 |  * this program; if not, write to the Free Software Foundation, Inc., | 
 | 18 |  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA | 
 | 19 |  */ | 
 | 20 |  | 
 | 21 | #include <linux/kernel.h> | 
 | 22 | #include <linux/module.h> | 
 | 23 | #include <linux/platform_device.h> | 
 | 24 | #include <linux/err.h> | 
 | 25 | #include <linux/hwmon.h> | 
 | 26 | #include <linux/hwmon-sysfs.h> | 
 | 27 | #include <linux/slab.h> | 
 | 28 |  | 
 | 29 | #include <linux/mfd/wm831x/core.h> | 
 | 30 | #include <linux/mfd/wm831x/auxadc.h> | 
 | 31 |  | 
 | 32 | struct wm831x_hwmon { | 
 | 33 | 	struct wm831x *wm831x; | 
 | 34 | 	struct device *classdev; | 
 | 35 | }; | 
 | 36 |  | 
 | 37 | static ssize_t show_name(struct device *dev, | 
 | 38 | 			 struct device_attribute *attr, char *buf) | 
 | 39 | { | 
 | 40 | 	return sprintf(buf, "wm831x\n"); | 
 | 41 | } | 
 | 42 |  | 
 | 43 | static const char * const input_names[] = { | 
 | 44 | 	[WM831X_AUX_SYSVDD]    = "SYSVDD", | 
 | 45 | 	[WM831X_AUX_USB]       = "USB", | 
 | 46 | 	[WM831X_AUX_BKUP_BATT] = "Backup battery", | 
 | 47 | 	[WM831X_AUX_BATT]      = "Battery", | 
 | 48 | 	[WM831X_AUX_WALL]      = "WALL", | 
 | 49 | 	[WM831X_AUX_CHIP_TEMP] = "PMIC", | 
 | 50 | 	[WM831X_AUX_BATT_TEMP] = "Battery", | 
 | 51 | }; | 
 | 52 |  | 
 | 53 |  | 
 | 54 | static ssize_t show_voltage(struct device *dev, | 
 | 55 | 			    struct device_attribute *attr, char *buf) | 
 | 56 | { | 
 | 57 | 	struct wm831x_hwmon *hwmon = dev_get_drvdata(dev); | 
 | 58 | 	int channel = to_sensor_dev_attr(attr)->index; | 
 | 59 | 	int ret; | 
 | 60 |  | 
 | 61 | 	ret = wm831x_auxadc_read_uv(hwmon->wm831x, channel); | 
 | 62 | 	if (ret < 0) | 
 | 63 | 		return ret; | 
 | 64 |  | 
 | 65 | 	return sprintf(buf, "%d\n", DIV_ROUND_CLOSEST(ret, 1000)); | 
 | 66 | } | 
 | 67 |  | 
 | 68 | static ssize_t show_chip_temp(struct device *dev, | 
 | 69 | 			      struct device_attribute *attr, char *buf) | 
 | 70 | { | 
 | 71 | 	struct wm831x_hwmon *hwmon = dev_get_drvdata(dev); | 
 | 72 | 	int channel = to_sensor_dev_attr(attr)->index; | 
 | 73 | 	int ret; | 
 | 74 |  | 
 | 75 | 	ret = wm831x_auxadc_read(hwmon->wm831x, channel); | 
 | 76 | 	if (ret < 0) | 
 | 77 | 		return ret; | 
 | 78 |  | 
 | 79 | 	/* Degrees celsius = (512.18-ret) / 1.0983 */ | 
 | 80 | 	ret = 512180 - (ret * 1000); | 
 | 81 | 	ret = DIV_ROUND_CLOSEST(ret * 10000, 10983); | 
 | 82 |  | 
 | 83 | 	return sprintf(buf, "%d\n", ret); | 
 | 84 | } | 
 | 85 |  | 
 | 86 | static ssize_t show_label(struct device *dev, | 
 | 87 | 			  struct device_attribute *attr, char *buf) | 
 | 88 | { | 
 | 89 | 	int channel = to_sensor_dev_attr(attr)->index; | 
 | 90 |  | 
 | 91 | 	return sprintf(buf, "%s\n", input_names[channel]); | 
 | 92 | } | 
 | 93 |  | 
 | 94 | #define WM831X_VOLTAGE(id, name) \ | 
 | 95 | 	static SENSOR_DEVICE_ATTR(in##id##_input, S_IRUGO, show_voltage, \ | 
 | 96 | 				  NULL, name) | 
 | 97 |  | 
 | 98 | #define WM831X_NAMED_VOLTAGE(id, name) \ | 
 | 99 | 	WM831X_VOLTAGE(id, name); \ | 
 | 100 | 	static SENSOR_DEVICE_ATTR(in##id##_label, S_IRUGO, show_label,	\ | 
 | 101 | 				  NULL, name) | 
 | 102 |  | 
 | 103 | static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); | 
 | 104 |  | 
 | 105 | WM831X_VOLTAGE(0, WM831X_AUX_AUX1); | 
 | 106 | WM831X_VOLTAGE(1, WM831X_AUX_AUX2); | 
 | 107 | WM831X_VOLTAGE(2, WM831X_AUX_AUX3); | 
 | 108 | WM831X_VOLTAGE(3, WM831X_AUX_AUX4); | 
 | 109 |  | 
 | 110 | WM831X_NAMED_VOLTAGE(4, WM831X_AUX_SYSVDD); | 
 | 111 | WM831X_NAMED_VOLTAGE(5, WM831X_AUX_USB); | 
 | 112 | WM831X_NAMED_VOLTAGE(6, WM831X_AUX_BATT); | 
 | 113 | WM831X_NAMED_VOLTAGE(7, WM831X_AUX_WALL); | 
 | 114 | WM831X_NAMED_VOLTAGE(8, WM831X_AUX_BKUP_BATT); | 
 | 115 |  | 
 | 116 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_chip_temp, NULL, | 
 | 117 | 			  WM831X_AUX_CHIP_TEMP); | 
 | 118 | static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, show_label, NULL, | 
 | 119 | 			  WM831X_AUX_CHIP_TEMP); | 
 | 120 | /* | 
 | 121 |  * Report as a voltage since conversion depends on external components | 
 | 122 |  * and that's what the ABI wants. | 
 | 123 |  */ | 
 | 124 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_voltage, NULL, | 
 | 125 | 			  WM831X_AUX_BATT_TEMP); | 
 | 126 | static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, show_label, NULL, | 
 | 127 | 			  WM831X_AUX_BATT_TEMP); | 
 | 128 |  | 
 | 129 | static struct attribute *wm831x_attributes[] = { | 
 | 130 | 	&dev_attr_name.attr, | 
 | 131 |  | 
 | 132 | 	&sensor_dev_attr_in0_input.dev_attr.attr, | 
 | 133 | 	&sensor_dev_attr_in1_input.dev_attr.attr, | 
 | 134 | 	&sensor_dev_attr_in2_input.dev_attr.attr, | 
 | 135 | 	&sensor_dev_attr_in3_input.dev_attr.attr, | 
 | 136 |  | 
 | 137 | 	&sensor_dev_attr_in4_input.dev_attr.attr, | 
 | 138 | 	&sensor_dev_attr_in4_label.dev_attr.attr, | 
 | 139 | 	&sensor_dev_attr_in5_input.dev_attr.attr, | 
 | 140 | 	&sensor_dev_attr_in5_label.dev_attr.attr, | 
 | 141 | 	&sensor_dev_attr_in6_input.dev_attr.attr, | 
 | 142 | 	&sensor_dev_attr_in6_label.dev_attr.attr, | 
 | 143 | 	&sensor_dev_attr_in7_input.dev_attr.attr, | 
 | 144 | 	&sensor_dev_attr_in7_label.dev_attr.attr, | 
 | 145 | 	&sensor_dev_attr_in8_input.dev_attr.attr, | 
 | 146 | 	&sensor_dev_attr_in8_label.dev_attr.attr, | 
 | 147 |  | 
 | 148 | 	&sensor_dev_attr_temp1_input.dev_attr.attr, | 
 | 149 | 	&sensor_dev_attr_temp1_label.dev_attr.attr, | 
 | 150 | 	&sensor_dev_attr_temp2_input.dev_attr.attr, | 
 | 151 | 	&sensor_dev_attr_temp2_label.dev_attr.attr, | 
 | 152 |  | 
 | 153 | 	NULL | 
 | 154 | }; | 
 | 155 |  | 
 | 156 | static const struct attribute_group wm831x_attr_group = { | 
 | 157 | 	.attrs	= wm831x_attributes, | 
 | 158 | }; | 
 | 159 |  | 
 | 160 | static int __devinit wm831x_hwmon_probe(struct platform_device *pdev) | 
 | 161 | { | 
 | 162 | 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent); | 
 | 163 | 	struct wm831x_hwmon *hwmon; | 
 | 164 | 	int ret; | 
 | 165 |  | 
 | 166 | 	hwmon = kzalloc(sizeof(struct wm831x_hwmon), GFP_KERNEL); | 
 | 167 | 	if (!hwmon) | 
 | 168 | 		return -ENOMEM; | 
 | 169 |  | 
 | 170 | 	hwmon->wm831x = wm831x; | 
 | 171 |  | 
 | 172 | 	ret = sysfs_create_group(&pdev->dev.kobj, &wm831x_attr_group); | 
 | 173 | 	if (ret) | 
 | 174 | 		goto err; | 
 | 175 |  | 
 | 176 | 	hwmon->classdev = hwmon_device_register(&pdev->dev); | 
 | 177 | 	if (IS_ERR(hwmon->classdev)) { | 
 | 178 | 		ret = PTR_ERR(hwmon->classdev); | 
 | 179 | 		goto err_sysfs; | 
 | 180 | 	} | 
 | 181 |  | 
 | 182 | 	platform_set_drvdata(pdev, hwmon); | 
 | 183 |  | 
 | 184 | 	return 0; | 
 | 185 |  | 
 | 186 | err_sysfs: | 
 | 187 | 	sysfs_remove_group(&pdev->dev.kobj, &wm831x_attr_group); | 
 | 188 | err: | 
 | 189 | 	kfree(hwmon); | 
 | 190 | 	return ret; | 
 | 191 | } | 
 | 192 |  | 
 | 193 | static int __devexit wm831x_hwmon_remove(struct platform_device *pdev) | 
 | 194 | { | 
 | 195 | 	struct wm831x_hwmon *hwmon = platform_get_drvdata(pdev); | 
 | 196 |  | 
 | 197 | 	hwmon_device_unregister(hwmon->classdev); | 
 | 198 | 	sysfs_remove_group(&pdev->dev.kobj, &wm831x_attr_group); | 
 | 199 | 	platform_set_drvdata(pdev, NULL); | 
 | 200 | 	kfree(hwmon); | 
 | 201 |  | 
 | 202 | 	return 0; | 
 | 203 | } | 
 | 204 |  | 
 | 205 | static struct platform_driver wm831x_hwmon_driver = { | 
 | 206 | 	.probe = wm831x_hwmon_probe, | 
 | 207 | 	.remove = __devexit_p(wm831x_hwmon_remove), | 
 | 208 | 	.driver = { | 
 | 209 | 		.name = "wm831x-hwmon", | 
 | 210 | 		.owner = THIS_MODULE, | 
 | 211 | 	}, | 
 | 212 | }; | 
 | 213 |  | 
 | 214 | module_platform_driver(wm831x_hwmon_driver); | 
 | 215 |  | 
 | 216 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); | 
 | 217 | MODULE_DESCRIPTION("WM831x Hardware Monitoring"); | 
 | 218 | MODULE_LICENSE("GPL"); | 
 | 219 | MODULE_ALIAS("platform:wm831x-hwmon"); |