f4b8d5d1d62a6ece93a7f78ffff4128665f8b867
[openwrt/staging/ldir.git] / target / linux / bcm27xx / patches-5.4 / 950-0772-w1_therm-adding-alarm-sysfs-entry.patch
1 From 8c3d9356c3c3b9c9188f1e192c7e2541e313c7ab Mon Sep 17 00:00:00 2001
2 From: Akira Shimahara <akira215corp@gmail.com>
3 Date: Mon, 11 May 2020 22:38:01 +0200
4 Subject: [PATCH] w1_therm: adding alarm sysfs entry
5
6 commit e2c94d6f572079511945e64537eb1218643f2e68 upstream.
7
8 Adding device alarms settings by a dedicated sysfs entry alarms (RW):
9 read or write TH and TL in the device RAM. Checking devices in alarm
10 state could be performed using the master search command.
11
12 As alarms temperature level are store in a 8 bit register on the device
13 and are signed values, a safe cast shall be performed using the min and
14 max temperature that device are able to measure. This is done by
15 int_to_short inline function.
16
17 A 'write_data' field is added in the device structure, to bind the
18 correct writing function, as some devices may have 2 or 3 bytes RAM.
19
20 Updating Documentation/ABI/testing/sysfs-driver-w1_therm accordingly.
21
22 Signed-off-by: Akira Shimahara <akira215corp@gmail.com>
23 Link: https://lore.kernel.org/r/20200511203801.411253-1-akira215corp@gmail.com
24 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
25 ---
26 .../ABI/testing/sysfs-driver-w1_therm | 16 ++
27 drivers/w1/slaves/w1_therm.c | 161 ++++++++++++++++++
28 2 files changed, 177 insertions(+)
29
30 --- a/Documentation/ABI/testing/sysfs-driver-w1_therm
31 +++ b/Documentation/ABI/testing/sysfs-driver-w1_therm
32 @@ -1,3 +1,19 @@
33 +What: /sys/bus/w1/devices/.../alarms
34 +Date: May 2020
35 +Contact: Akira Shimahara <akira215corp@gmail.com>
36 +Description:
37 + (RW) read or write TH and TL (Temperature High an Low) alarms.
38 + Values shall be space separated and in the device range
39 + (typical -55 degC to 125 degC), if not values will be trimmed
40 + to device min/max capabilities. Values are integer as they are
41 + stored in a 8bit register in the device. Lowest value is
42 + automatically put to TL. Once set, alarms could be search at
43 + master level, refer to Documentation/w1/w1_generic.rst for
44 + detailed information
45 +Users: any user space application which wants to communicate with
46 + w1_term device
47 +
48 +
49 What: /sys/bus/w1/devices/.../eeprom
50 Date: May 2020
51 Contact: Akira Shimahara <akira215corp@gmail.com>
52 --- a/drivers/w1/slaves/w1_therm.c
53 +++ b/drivers/w1/slaves/w1_therm.c
54 @@ -58,6 +58,9 @@ module_param_named(strong_pullup, w1_str
55 #define EEPROM_CMD_WRITE "save" /* cmd for write eeprom sysfs */
56 #define EEPROM_CMD_READ "restore" /* cmd for read eeprom sysfs */
57
58 +#define MIN_TEMP -55 /* min temperature that can be mesured */
59 +#define MAX_TEMP 125 /* max temperature that can be mesured */
60 +
61 /* Helpers Macros */
62
63 /*
64 @@ -96,6 +99,7 @@ module_param_named(strong_pullup, w1_str
65 * @get_conversion_time: pointer to the device conversion time function
66 * @set_resolution: pointer to the device set_resolution function
67 * @get_resolution: pointer to the device get_resolution function
68 + * @write_data: pointer to the device writing function (2 or 3 bytes)
69 */
70 struct w1_therm_family_converter {
71 u8 broken;
72 @@ -105,6 +109,7 @@ struct w1_therm_family_converter {
73 int (*get_conversion_time)(struct w1_slave *sl);
74 int (*set_resolution)(struct w1_slave *sl, int val);
75 int (*get_resolution)(struct w1_slave *sl);
76 + int (*write_data)(struct w1_slave *sl, const u8 *data);
77 };
78
79 /**
80 @@ -239,6 +244,12 @@ static ssize_t resolution_store(struct d
81 static ssize_t eeprom_store(struct device *device,
82 struct device_attribute *attr, const char *buf, size_t size);
83
84 +static ssize_t alarms_store(struct device *device,
85 + struct device_attribute *attr, const char *buf, size_t size);
86 +
87 +static ssize_t alarms_show(struct device *device,
88 + struct device_attribute *attr, char *buf);
89 +
90 /* Attributes declarations */
91
92 static DEVICE_ATTR_RW(w1_slave);
93 @@ -247,6 +258,7 @@ static DEVICE_ATTR_RO(temperature);
94 static DEVICE_ATTR_RO(ext_power);
95 static DEVICE_ATTR_RW(resolution);
96 static DEVICE_ATTR_WO(eeprom);
97 +static DEVICE_ATTR_RW(alarms);
98
99 /* Interface Functions declaration */
100
101 @@ -278,6 +290,7 @@ static struct attribute *w1_therm_attrs[
102 &dev_attr_ext_power.attr,
103 &dev_attr_resolution.attr,
104 &dev_attr_eeprom.attr,
105 + &dev_attr_alarms.attr,
106 NULL,
107 };
108
109 @@ -286,6 +299,7 @@ static struct attribute *w1_ds18s20_attr
110 &dev_attr_temperature.attr,
111 &dev_attr_ext_power.attr,
112 &dev_attr_eeprom.attr,
113 + &dev_attr_alarms.attr,
114 NULL,
115 };
116
117 @@ -296,6 +310,7 @@ static struct attribute *w1_ds28ea00_att
118 &dev_attr_ext_power.attr,
119 &dev_attr_resolution.attr,
120 &dev_attr_eeprom.attr,
121 + &dev_attr_alarms.attr,
122 NULL,
123 };
124
125 @@ -556,6 +571,7 @@ static struct w1_therm_family_converter
126 .get_conversion_time = w1_DS18S20_convert_time,
127 .set_resolution = NULL, /* no config register */
128 .get_resolution = NULL, /* no config register */
129 + .write_data = w1_DS18S20_write_data,
130 },
131 {
132 .f = &w1_therm_family_DS1822,
133 @@ -563,6 +579,7 @@ static struct w1_therm_family_converter
134 .get_conversion_time = w1_DS18B20_convert_time,
135 .set_resolution = w1_DS18B20_set_resolution,
136 .get_resolution = w1_DS18B20_get_resolution,
137 + .write_data = w1_DS18B20_write_data,
138 },
139 {
140 .f = &w1_therm_family_DS18B20,
141 @@ -570,6 +587,7 @@ static struct w1_therm_family_converter
142 .get_conversion_time = w1_DS18B20_convert_time,
143 .set_resolution = w1_DS18B20_set_resolution,
144 .get_resolution = w1_DS18B20_get_resolution,
145 + .write_data = w1_DS18B20_write_data,
146 },
147 {
148 .f = &w1_therm_family_DS28EA00,
149 @@ -577,6 +595,7 @@ static struct w1_therm_family_converter
150 .get_conversion_time = w1_DS18B20_convert_time,
151 .set_resolution = w1_DS18B20_set_resolution,
152 .get_resolution = w1_DS18B20_get_resolution,
153 + .write_data = w1_DS18B20_write_data,
154 },
155 {
156 .f = &w1_therm_family_DS1825,
157 @@ -584,6 +603,7 @@ static struct w1_therm_family_converter
158 .get_conversion_time = w1_DS18B20_convert_time,
159 .set_resolution = w1_DS18B20_set_resolution,
160 .get_resolution = w1_DS18B20_get_resolution,
161 + .write_data = w1_DS18B20_write_data,
162 }
163 };
164
165 @@ -678,6 +698,26 @@ static inline int temperature_from_RAM(s
166 return 0; /* No device family */
167 }
168
169 +/**
170 + * int_to_short() - Safe casting of int to short
171 + *
172 + * @i: integer to be converted to short
173 + *
174 + * Device register use 1 byte to store signed integer.
175 + * This helper function convert the int in a signed short,
176 + * using the min/max values that device can measure as limits.
177 + * min/max values are defined by macro.
178 + *
179 + * Return: a short in the range of min/max value
180 + */
181 +static inline s8 int_to_short(int i)
182 +{
183 + /* Prepare to cast to short by eliminating out of range values */
184 + i = i > MAX_TEMP ? MAX_TEMP : i;
185 + i = i < MIN_TEMP ? MIN_TEMP : i;
186 + return (s8) i;
187 +}
188 +
189 /* Interface Functions */
190
191 static int w1_therm_add_slave(struct w1_slave *sl)
192 @@ -1250,6 +1290,127 @@ static ssize_t eeprom_store(struct devic
193
194 return size;
195 }
196 +
197 +static ssize_t alarms_show(struct device *device,
198 + struct device_attribute *attr, char *buf)
199 +{
200 + struct w1_slave *sl = dev_to_w1_slave(device);
201 + int ret = -ENODEV;
202 + s8 th = 0, tl = 0;
203 + struct therm_info scratchpad;
204 +
205 + ret = read_scratchpad(sl, &scratchpad);
206 +
207 + if (!ret) {
208 + th = scratchpad.rom[2]; /* TH is byte 2 */
209 + tl = scratchpad.rom[3]; /* TL is byte 3 */
210 + } else {
211 + dev_info(device,
212 + "%s: error reading alarms register %d\n",
213 + __func__, ret);
214 + }
215 +
216 + return sprintf(buf, "%hd %hd\n", tl, th);
217 +}
218 +
219 +static ssize_t alarms_store(struct device *device,
220 + struct device_attribute *attr, const char *buf, size_t size)
221 +{
222 + struct w1_slave *sl = dev_to_w1_slave(device);
223 + struct therm_info info;
224 + u8 new_config_register[3]; /* array of data to be written */
225 + int temp, ret = -EINVAL;
226 + char *token = NULL;
227 + s8 tl, th, tt; /* 1 byte per value + temp ring order */
228 + char *p_args = kmalloc(size, GFP_KERNEL);
229 +
230 + /* Safe string copys as buf is const */
231 + if (!p_args) {
232 + dev_warn(device,
233 + "%s: error unable to allocate memory %d\n",
234 + __func__, -ENOMEM);
235 + return size;
236 + }
237 + strcpy(p_args, buf);
238 +
239 + /* Split string using space char */
240 + token = strsep(&p_args, " ");
241 +
242 + if (!token) {
243 + dev_info(device,
244 + "%s: error parsing args %d\n", __func__, -EINVAL);
245 + goto free_m;
246 + }
247 +
248 + /* Convert 1st entry to int */
249 + ret = kstrtoint (token, 10, &temp);
250 + if (ret) {
251 + dev_info(device,
252 + "%s: error parsing args %d\n", __func__, ret);
253 + goto free_m;
254 + }
255 +
256 + tl = int_to_short(temp);
257 +
258 + /* Split string using space char */
259 + token = strsep(&p_args, " ");
260 + if (!token) {
261 + dev_info(device,
262 + "%s: error parsing args %d\n", __func__, -EINVAL);
263 + goto free_m;
264 + }
265 + /* Convert 2nd entry to int */
266 + ret = kstrtoint (token, 10, &temp);
267 + if (ret) {
268 + dev_info(device,
269 + "%s: error parsing args %d\n", __func__, ret);
270 + goto free_m;
271 + }
272 +
273 + /* Prepare to cast to short by eliminating out of range values */
274 + th = int_to_short(temp);
275 +
276 + /* Reorder if required th and tl */
277 + if (tl > th) {
278 + tt = tl; tl = th; th = tt;
279 + }
280 +
281 + /*
282 + * Read the scratchpad to change only the required bits
283 + * (th : byte 2 - tl: byte 3)
284 + */
285 + ret = read_scratchpad(sl, &info);
286 + if (!ret) {
287 + new_config_register[0] = th; /* Byte 2 */
288 + new_config_register[1] = tl; /* Byte 3 */
289 + new_config_register[2] = info.rom[4];/* Byte 4 */
290 + } else {
291 + dev_info(device,
292 + "%s: error reading from the slave device %d\n",
293 + __func__, ret);
294 + goto free_m;
295 + }
296 +
297 + /* Write data in the device RAM */
298 + if (!SLAVE_SPECIFIC_FUNC(sl)) {
299 + dev_info(device,
300 + "%s: Device not supported by the driver %d\n",
301 + __func__, -ENODEV);
302 + goto free_m;
303 + }
304 +
305 + ret = SLAVE_SPECIFIC_FUNC(sl)->write_data(sl, new_config_register);
306 + if (ret)
307 + dev_info(device,
308 + "%s: error writing to the slave device %d\n",
309 + __func__, ret);
310 +
311 +free_m:
312 + /* free allocated memory */
313 + kfree(p_args);
314 +
315 + return size;
316 +}
317
318 #if IS_REACHABLE(CONFIG_HWMON)
319 static int w1_read_temp(struct device *device, u32 attr, int channel,