f25495290dc7f16ae134864ffbbb17250446e4c4
[openwrt/staging/pepe2k.git] /
1 From 23395c6df3c8ef7b6e43b4968de402169ec7c755 Mon Sep 17 00:00:00 2001
2 From: Michael Shych <michaelsh@nvidia.com>
3 Date: Sat, 30 Apr 2022 14:49:03 +0300
4 Subject: [PATCH] hwmon: (emc2305) add support for EMC2301/2/3/5
5 RPM-based PWM Fan Speed Controller.
6
7 Submitted to linux-hwmon mailing list at
8 https://patchwork.kernel.org/project/linux-hwmon/patch/20220430114905.53448-2-michaelsh@nvidia.com/
9
10 Add driver for Microchip EMC2301/2/3/5 RPM-based PWM Fan Speed Controller.
11 Modify Makefile and Kconfig to support Microchip EMC2305 RPM-based
12 PWM Fan Speed Controller.
13
14 Signed-off-by: Michael Shych <michaelsh@nvidia.com>
15 Reviewed-by: Vadim Pasternak <vadimp@nvidia.com>
16 ---
17 drivers/hwmon/Kconfig | 13 +
18 drivers/hwmon/Makefile | 1 +
19 drivers/hwmon/emc2305.c | 629 ++++++++++++++++++++++++++++++++++++++++
20 3 files changed, 643 insertions(+)
21 create mode 100644 drivers/hwmon/emc2305.c
22
23 --- a/drivers/hwmon/Kconfig
24 +++ b/drivers/hwmon/Kconfig
25 @@ -1685,6 +1685,19 @@ config SENSORS_EMC2103
26 This driver can also be built as a module. If so, the module
27 will be called emc2103.
28
29 +config SENSORS_EMC2305
30 + tristate "Microchip EMC2305 and compatible EMC2301/2/3"
31 + depends on I2C
32 + imply THERMAL
33 + help
34 + If you say yes here you get support for the Microchip EMC2305
35 + fan controller chips.
36 + The Microchip EMC2305 is a fan controller for up to 5 fans.
37 + Fan rotation speeds are reported in RPM.
38 +
39 + This driver can also be built as a module. If so, the module
40 + will be called emc2305.
41 +
42 config SENSORS_EMC6W201
43 tristate "SMSC EMC6W201"
44 depends on I2C
45 --- a/drivers/hwmon/Makefile
46 +++ b/drivers/hwmon/Makefile
47 @@ -67,6 +67,7 @@ obj-$(CONFIG_SENSORS_DS620) += ds620.o
48 obj-$(CONFIG_SENSORS_DS1621) += ds1621.o
49 obj-$(CONFIG_SENSORS_EMC1403) += emc1403.o
50 obj-$(CONFIG_SENSORS_EMC2103) += emc2103.o
51 +obj-$(CONFIG_SENSORS_EMC2305) += emc2305.o
52 obj-$(CONFIG_SENSORS_EMC6W201) += emc6w201.o
53 obj-$(CONFIG_SENSORS_F71805F) += f71805f.o
54 obj-$(CONFIG_SENSORS_F71882FG) += f71882fg.o
55 --- /dev/null
56 +++ b/drivers/hwmon/emc2305.c
57 @@ -0,0 +1,629 @@
58 +// SPDX-License-Identifier: GPL-2.0+
59 +/*
60 + * Hardware monitoring driver for EMC2305 fan controller
61 + *
62 + * Copyright (C) 2022 Nvidia Technologies Ltd and Delta Networks, Inc.
63 + */
64 +
65 +#include <linux/err.h>
66 +#include <linux/hwmon.h>
67 +#include <linux/hwmon-sysfs.h>
68 +#include <linux/i2c.h>
69 +#include <linux/module.h>
70 +#include <linux/of.h>
71 +#include <linux/thermal.h>
72 +#include <linux/version.h>
73 +
74 +static const unsigned short
75 +emc2305_normal_i2c[] = { 0x27, 0x2c, 0x2d, 0x2e, 0x2f, 0x4c, 0x4d, I2C_CLIENT_END };
76 +
77 +#define EMC2305_REG_DRIVE_FAIL_STATUS 0x27
78 +#define EMC2305_REG_DEVICE 0xfd
79 +#define EMC2305_REG_VENDOR 0xfe
80 +#define EMC2305_FAN_MAX 0xff
81 +#define EMC2305_FAN_MIN 0x00
82 +#define EMC2305_FAN_MAX_STATE 10
83 +#define EMC2305_DEVICE 0x34
84 +#define EMC2305_VENDOR 0x5d
85 +#define EMC2305_REG_PRODUCT_ID 0xfd
86 +#define EMC2305_TACH_REGS_UNUSE_BITS 3
87 +#define EMC2305_TACH_CNT_MULTIPLIER 0x02
88 +#define EMC2305_PWM_MAX 5
89 +#define EMC2305_PWM_CHNL_CMN 0
90 +
91 +#define EMC2305_PWM_DUTY2STATE(duty, max_state, pwm_max) \
92 + (DIV_ROUND_CLOSEST((duty) * (max_state), (pwm_max)))
93 +#define EMC2305_PWM_STATE2DUTY(state, max_state, pwm_max) \
94 + (DIV_ROUND_CLOSEST((state) * (pwm_max), (max_state)))
95 +
96 +/* Factor by equations [2] and [3] from data sheet; valid for fans where the number of edges
97 + * equal (poles * 2 + 1).
98 + */
99 +#define EMC2305_RPM_FACTOR 3932160
100 +
101 +#define EMC2305_REG_FAN_DRIVE(n) (0x30 + 0x10 * (n))
102 +#define EMC2305_REG_FAN_MIN_DRIVE(n) (0x38 + 0x10 * (n))
103 +#define EMC2305_REG_FAN_TACH(n) (0x3e + 0x10 * (n))
104 +
105 +enum emc230x_product_id {
106 + EMC2305 = 0x34,
107 + EMC2303 = 0x35,
108 + EMC2302 = 0x36,
109 + EMC2301 = 0x37,
110 +};
111 +
112 +static const struct i2c_device_id emc2305_ids[] = {
113 + { "emc2305", 0 },
114 + { "emc2303", 0 },
115 + { "emc2302", 0 },
116 + { "emc2301", 0 },
117 + { }
118 +};
119 +MODULE_DEVICE_TABLE(i2c, emc2305_ids);
120 +
121 +static const struct of_device_id emc2305_dt_ids[] = {
122 + { .compatible = "smsc,emc2305" },
123 + { }
124 +};
125 +MODULE_DEVICE_TABLE(of, emc2305_dt_ids);
126 +
127 +/**
128 + * @cdev: cooling device;
129 + * @curr_state: cooling current state;
130 + * @last_hwmon_state: last cooling state updated by hwmon subsystem;
131 + * @last_thermal_state: last cooling state updated by thermal subsystem;
132 + *
133 + * The 'last_hwmon_state' and 'last_thermal_state' fields are provided to support fan low limit
134 + * speed feature. The purpose of this feature is to provides ability to limit fan speed
135 + * according to some system wise considerations, like absence of some replaceable units (PSU or
136 + * line cards), high system ambient temperature, unreliable transceivers temperature sensing or
137 + * some other factors which indirectly impacts system's airflow
138 + * Fan low limit feature is supported through 'hwmon' interface: 'hwmon' 'pwm' attribute is
139 + * used for setting low limit for fan speed in case 'thermal' subsystem is configured in
140 + * kernel. In this case setting fan speed through 'hwmon' will never let the 'thermal'
141 + * subsystem to select a lower duty cycle than the duty cycle selected with the 'pwm'
142 + * attribute.
143 + * From other side, fan speed is to be updated in hardware through 'pwm' only in case the
144 + * requested fan speed is above last speed set by 'thermal' subsystem, otherwise requested fan
145 + * speed will be just stored with no PWM update.
146 + */
147 +struct emc2305_cdev_data {
148 + struct thermal_cooling_device *cdev;
149 + unsigned int cur_state;
150 + unsigned long last_hwmon_state;
151 + unsigned long last_thermal_state;
152 +};
153 +
154 +/**
155 + * @client: i2c client;
156 + * @hwmon_dev: hwmon device;
157 + * @max_state: maximum cooling state of the cooling device;
158 + * @pwm_max: maximum PWM;
159 + * @pwm_min: minimum PWM;
160 + * @pwm_channel: maximum number of PWM channels;
161 + * @cdev_data: array of cooling devices data;
162 + */
163 +struct emc2305_data {
164 + struct i2c_client *client;
165 + struct device *hwmon_dev;
166 + u8 max_state;
167 + u8 pwm_max;
168 + u8 pwm_min;
169 + u8 pwm_num;
170 + u8 pwm_channel;
171 + struct emc2305_cdev_data cdev_data[EMC2305_PWM_MAX];
172 +};
173 +
174 +static char *emc2305_fan_name[] = {
175 + "emc2305_fan",
176 + "emc2305_fan1",
177 + "emc2305_fan2",
178 + "emc2305_fan3",
179 + "emc2305_fan4",
180 + "emc2305_fan5",
181 +};
182 +
183 +static int emc2305_get_max_channel(struct emc2305_data *data)
184 +{
185 + if (data->pwm_channel == EMC2305_PWM_CHNL_CMN)
186 + return data->pwm_num;
187 + else
188 + return data->pwm_channel;
189 +}
190 +
191 +static int emc2305_get_cdev_idx(struct thermal_cooling_device *cdev)
192 +{
193 + struct emc2305_data *data = cdev->devdata;
194 + size_t len = strlen(cdev->type);
195 + int ret;
196 +
197 + if (len <= 0)
198 + return -EINVAL;
199 +
200 + /* Retuns index of cooling device 0..4 in case of separate PWM setting.
201 + * Zero index is used in case of one common PWM setting.
202 + * If the mode is set as EMC2305_PWM_CHNL_CMN, all PWMs are to be bound
203 + * to the common thermal zone and should work at the same speed
204 + * to perform cooling for the same thermal junction.
205 + * Otherwise, return specific channel that will be used in bound
206 + * related PWM to the thermal zone.
207 + */
208 + if (data->pwm_channel == EMC2305_PWM_CHNL_CMN)
209 + return 0;
210 +
211 + ret = cdev->type[len - 1];
212 + switch (ret) {
213 + case '1' ... '5':
214 + return ret - '1';
215 + default:
216 + break;
217 + }
218 + return -EINVAL;
219 +}
220 +
221 +static int emc2305_get_cur_state(struct thermal_cooling_device *cdev, unsigned long *state)
222 +{
223 + int cdev_idx;
224 + struct emc2305_data *data = cdev->devdata;
225 +
226 + cdev_idx = emc2305_get_cdev_idx(cdev);
227 + if (cdev_idx < 0)
228 + return cdev_idx;
229 +
230 + *state = data->cdev_data[cdev_idx].cur_state;
231 + return 0;
232 +}
233 +
234 +static int emc2305_get_max_state(struct thermal_cooling_device *cdev, unsigned long *state)
235 +{
236 + struct emc2305_data *data = cdev->devdata;
237 + *state = data->max_state;
238 + return 0;
239 +}
240 +
241 +static int emc2305_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
242 +{
243 + int cdev_idx;
244 + struct emc2305_data *data = cdev->devdata;
245 + struct i2c_client *client = data->client;
246 + u8 val, i;
247 +
248 + if (state > data->max_state)
249 + return -EINVAL;
250 +
251 + cdev_idx = emc2305_get_cdev_idx(cdev);
252 + if (cdev_idx < 0)
253 + return cdev_idx;
254 +
255 + /* Save thermal state. */
256 + data->cdev_data[cdev_idx].last_thermal_state = state;
257 + state = max_t(unsigned long, state, data->cdev_data[cdev_idx].last_hwmon_state);
258 +
259 + val = EMC2305_PWM_STATE2DUTY(state, data->max_state, data->pwm_max);
260 + if (val > EMC2305_FAN_MAX)
261 + return -EINVAL;
262 +
263 + data->cdev_data[cdev_idx].cur_state = state;
264 + if (data->pwm_channel == EMC2305_PWM_CHNL_CMN)
265 + /* Set the same PWM value in all channels
266 + * if common PWM channel is used.
267 + */
268 + for (i = 0; i < data->pwm_num; i++)
269 + i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(i), val);
270 + else
271 + i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(cdev_idx), val);
272 +
273 + return 0;
274 +}
275 +
276 +static const struct thermal_cooling_device_ops emc2305_cooling_ops = {
277 + .get_max_state = emc2305_get_max_state,
278 + .get_cur_state = emc2305_get_cur_state,
279 + .set_cur_state = emc2305_set_cur_state,
280 +};
281 +
282 +static int emc2305_show_fault(struct device *dev, int channel)
283 +{
284 + struct emc2305_data *data = dev_get_drvdata(dev);
285 + struct i2c_client *client = data->client;
286 + int status_reg;
287 +
288 + status_reg = i2c_smbus_read_byte_data(client, EMC2305_REG_DRIVE_FAIL_STATUS);
289 + return status_reg & (1 << channel) ? 1 : 0;
290 +}
291 +
292 +static int emc2305_show_fan(struct device *dev, int channel)
293 +{
294 + struct emc2305_data *data = dev_get_drvdata(dev);
295 + struct i2c_client *client = data->client;
296 + int ret;
297 +
298 + ret = i2c_smbus_read_word_swapped(client, EMC2305_REG_FAN_TACH(channel));
299 + if (ret < 0)
300 + return ret;
301 +
302 + ret = ret >> EMC2305_TACH_REGS_UNUSE_BITS;
303 + return EMC2305_RPM_FACTOR * EMC2305_TACH_CNT_MULTIPLIER / (ret > 0 ? ret : 1);
304 +}
305 +
306 +static int emc2305_show_pwm(struct device *dev, int channel)
307 +{
308 + struct emc2305_data *data = dev_get_drvdata(dev);
309 + struct i2c_client *client = data->client;
310 +
311 + return i2c_smbus_read_byte_data(client, EMC2305_REG_FAN_DRIVE(channel));
312 +}
313 +
314 +static int emc2305_set_pwm(struct device *dev, long val, int channel)
315 +{
316 + struct emc2305_data *data = dev_get_drvdata(dev);
317 + struct i2c_client *client = data->client;
318 +
319 + if (val < data->pwm_min || val > data->pwm_max)
320 + return -EINVAL;
321 +
322 + i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(channel), val);
323 + data->cdev_data[channel].cur_state = EMC2305_PWM_DUTY2STATE(val, data->max_state,
324 + data->pwm_max);
325 + return 0;
326 +}
327 +
328 +static int emc2305_get_tz_of(struct device *dev)
329 +{
330 + struct device_node *np = dev->of_node;
331 + struct emc2305_data *data = dev_get_drvdata(dev);
332 + int ret = 0;
333 +
334 + /* OF parameters are optional - overwrite default setting
335 + * if some of them are provided.
336 + */
337 +
338 + if (of_find_property(np, "emc2305,cooling-levels", NULL)) {
339 + ret = of_property_read_u8(np, "emc2305,cooling-levels", &data->max_state);
340 + if (ret)
341 + return ret;
342 + }
343 +
344 + if (of_find_property(np, "emc2305,pwm-max", NULL)) {
345 + ret = of_property_read_u8(np, "emc2305,pwm-max", &data->pwm_max);
346 + if (ret)
347 + return ret;
348 + }
349 +
350 + if (of_find_property(np, "emc2305,pwm-min", NULL)) {
351 + ret = of_property_read_u8(np, "emc2305,pwm-min", &data->pwm_min);
352 + if (ret)
353 + return ret;
354 + }
355 +
356 + /* Not defined or 0 means one thermal zone over all colling devices.
357 + * Otherwise - separted thermal zones for each PWM channel.
358 + */
359 + if (of_find_property(np, "emc2305,pwm-channel", NULL)) {
360 + ret = of_property_read_u8(np, "emc2305,pwm-channel", &data->pwm_channel);
361 + if (ret)
362 + return ret;
363 + }
364 +
365 + return ret;
366 +}
367 +
368 +static int emc2305_set_single_tz(struct device *dev, int idx)
369 +{
370 + struct emc2305_data *data = dev_get_drvdata(dev);
371 + long pwm = data->pwm_max;
372 + int cdev_idx;
373 +
374 + cdev_idx = (idx) ? idx - 1 : 0;
375 +
376 + if (dev->of_node)
377 + data->cdev_data[cdev_idx].cdev =
378 + devm_thermal_of_cooling_device_register(dev, dev->of_node,
379 + emc2305_fan_name[idx], data,
380 + &emc2305_cooling_ops);
381 + else
382 + data->cdev_data[cdev_idx].cdev =
383 + thermal_cooling_device_register(emc2305_fan_name[idx], data,
384 + &emc2305_cooling_ops);
385 +
386 + if (IS_ERR(data->cdev_data[cdev_idx].cdev)) {
387 + dev_err(dev, "Failed to register cooling device %s\n", emc2305_fan_name[idx]);
388 + return PTR_ERR(data->cdev_data[cdev_idx].cdev);
389 + }
390 + emc2305_set_pwm(dev, pwm, cdev_idx);
391 + data->cdev_data[cdev_idx].cur_state = data->max_state;
392 + /* Set minimal PWM speed. */
393 + data->cdev_data[cdev_idx].last_hwmon_state = EMC2305_PWM_DUTY2STATE(data->pwm_min,
394 + data->max_state,
395 + data->pwm_max);
396 + return 0;
397 +}
398 +
399 +static int emc2305_set_tz(struct device *dev)
400 +{
401 + struct emc2305_data *data = dev_get_drvdata(dev);
402 + int i, ret;
403 +
404 + if (data->pwm_channel == EMC2305_PWM_CHNL_CMN)
405 + return emc2305_set_single_tz(dev, 0);
406 +
407 + for (i = 0; i < data->pwm_channel; i++) {
408 + ret = emc2305_set_single_tz(dev, i + 1);
409 + if (ret)
410 + goto thermal_cooling_device_register_fail;
411 + }
412 + return 0;
413 +
414 +thermal_cooling_device_register_fail:
415 + emc2305_unset_tz(dev);
416 + return ret;
417 +}
418 +
419 +static void emc2305_unset_tz(struct device *dev)
420 +{
421 + struct emc2305_data *data = dev_get_drvdata(dev);
422 + int i;
423 +
424 + /* Unregister cooling device in case they have been registered by
425 + * thermal_cooling_device_unregister(). No need for clean-up flow in case they
426 + * have been registered by devm_thermal_of_cooling_device_register()
427 + */
428 + if (!dev->of_node) {
429 + for (i = 0; i < EMC2305_PWM_MAX; i++)
430 + if (data->cdev_data[i].cdev)
431 + thermal_cooling_device_unregister(data->cdev_data[i].cdev);
432 + }
433 +}
434 +
435 +static umode_t
436 +emc2305_is_visible(const void *data, enum hwmon_sensor_types type, u32 attr, int channel)
437 +{
438 + int max_channel = emc2305_get_max_channel((struct emc2305_data *)data);
439 +
440 + /* Don't show channels which are not physically connected. */
441 + if ((channel + 1) > max_channel)
442 + return 0;
443 + switch (type) {
444 + case hwmon_fan:
445 + switch (attr) {
446 + case hwmon_fan_input:
447 + return 0444;
448 + case hwmon_fan_fault:
449 + return 0444;
450 + default:
451 + break;
452 + }
453 + break;
454 + case hwmon_pwm:
455 + switch (attr) {
456 + case hwmon_pwm_input:
457 + return 0644;
458 + default:
459 + break;
460 + }
461 + break;
462 + default:
463 + break;
464 + }
465 +
466 + return 0;
467 +};
468 +
469 +static int
470 +emc2305_write(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long val)
471 +{
472 + struct emc2305_data *data = dev_get_drvdata(dev);
473 +
474 + switch (type) {
475 + case hwmon_pwm:
476 + switch (attr) {
477 + case hwmon_pwm_input:
478 + /* If thermal is configured - handle PWM limit setting. */
479 + if (IS_REACHABLE(CONFIG_THERMAL)) {
480 + data->cdev_data[channel].last_hwmon_state =
481 + EMC2305_PWM_DUTY2STATE(val, data->max_state, data->pwm_max);
482 + /* Update PWM only in case requested state is not less than the
483 + * last thermal state.
484 + */
485 + if (data->cdev_data[channel].last_hwmon_state >=
486 + data->cdev_data[channel].last_thermal_state)
487 + return emc2305_set_cur_state(data->cdev_data[channel].cdev,
488 + data->cdev_data[channel].last_hwmon_state);
489 + return 0;
490 + }
491 + return emc2305_set_pwm(dev, val, channel);
492 + default:
493 + break;
494 + }
495 + break;
496 + default:
497 + break;
498 + }
499 +
500 + return -EOPNOTSUPP;
501 +};
502 +
503 +static int
504 +emc2305_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long *val)
505 +{
506 + int ret;
507 +
508 + switch (type) {
509 + case hwmon_fan:
510 + switch (attr) {
511 + case hwmon_fan_input:
512 + ret = emc2305_show_fan(dev, channel);
513 + if (ret < 0)
514 + return ret;
515 + *val = ret;
516 + return 0;
517 + case hwmon_fan_fault:
518 + ret = emc2305_show_fault(dev, channel);
519 + if (ret < 0)
520 + return ret;
521 + *val = ret;
522 + return 0;
523 + default:
524 + break;
525 + }
526 + break;
527 + case hwmon_pwm:
528 + switch (attr) {
529 + case hwmon_pwm_input:
530 + ret = emc2305_show_pwm(dev, channel);
531 + if (ret < 0)
532 + return ret;
533 + *val = ret;
534 + return 0;
535 + default:
536 + break;
537 + }
538 + break;
539 + default:
540 + break;
541 + }
542 +
543 + return -EOPNOTSUPP;
544 +};
545 +
546 +static const struct hwmon_ops emc2305_ops = {
547 + .is_visible = emc2305_is_visible,
548 + .read = emc2305_read,
549 + .write = emc2305_write,
550 +};
551 +
552 +static const struct hwmon_channel_info *emc2305_info[] = {
553 + HWMON_CHANNEL_INFO(fan,
554 + HWMON_F_INPUT | HWMON_F_FAULT,
555 + HWMON_F_INPUT | HWMON_F_FAULT,
556 + HWMON_F_INPUT | HWMON_F_FAULT,
557 + HWMON_F_INPUT | HWMON_F_FAULT,
558 + HWMON_F_INPUT | HWMON_F_FAULT),
559 + HWMON_CHANNEL_INFO(pwm,
560 + HWMON_PWM_INPUT,
561 + HWMON_PWM_INPUT,
562 + HWMON_PWM_INPUT,
563 + HWMON_PWM_INPUT,
564 + HWMON_PWM_INPUT),
565 + NULL
566 +};
567 +
568 +static const struct hwmon_chip_info emc2305_chip_info = {
569 + .ops = &emc2305_ops,
570 + .info = emc2305_info,
571 +};
572 +
573 +static int emc2305_identify(struct device *dev)
574 +{
575 + struct i2c_client *client = to_i2c_client(dev);
576 + struct emc2305_data *data = i2c_get_clientdata(client);
577 + int ret;
578 +
579 + ret = i2c_smbus_read_byte_data(client, EMC2305_REG_PRODUCT_ID);
580 + if (ret < 0)
581 + return ret;
582 +
583 + switch (ret) {
584 + case EMC2305:
585 + data->pwm_num = 5;
586 + break;
587 + case EMC2303:
588 + data->pwm_num = 3;
589 + break;
590 + case EMC2302:
591 + data->pwm_num = 2;
592 + break;
593 + case EMC2301:
594 + data->pwm_num = 1;
595 + break;
596 + default:
597 + return -EINVAL;
598 + }
599 +
600 + return 0;
601 +}
602 +
603 +static int emc2305_probe(struct i2c_client *client, const struct i2c_device_id *id)
604 +{
605 + struct i2c_adapter *adapter = client->adapter;
606 + struct device *dev = &client->dev;
607 + struct emc2305_data *data;
608 + int vendor, device;
609 + int ret;
610 + int i;
611 +
612 + if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
613 + return -ENODEV;
614 +
615 + vendor = i2c_smbus_read_byte_data(client, EMC2305_REG_VENDOR);
616 + if (vendor != EMC2305_VENDOR)
617 + return -ENODEV;
618 +
619 + device = i2c_smbus_read_byte_data(client, EMC2305_REG_DEVICE);
620 + if (device != EMC2305_DEVICE)
621 + return -ENODEV;
622 +
623 + data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
624 + if (!data)
625 + return -ENOMEM;
626 +
627 + i2c_set_clientdata(client, data);
628 + data->client = client;
629 +
630 + ret = emc2305_identify(dev);
631 + if (ret)
632 + return ret;
633 +
634 + data->max_state = EMC2305_FAN_MAX_STATE;
635 + data->pwm_max = EMC2305_FAN_MAX;
636 + data->pwm_min = EMC2305_FAN_MIN;
637 + data->pwm_channel = EMC2305_PWM_CHNL_CMN;
638 + if (dev->of_node) {
639 + ret = emc2305_get_tz_of(dev);
640 + if (ret < 0)
641 + return ret;
642 + }
643 +
644 + data->hwmon_dev = devm_hwmon_device_register_with_info(dev, "emc2305", data,
645 + &emc2305_chip_info, NULL);
646 + if (IS_ERR(data->hwmon_dev))
647 + return PTR_ERR(data->hwmon_dev);
648 +
649 + if (IS_REACHABLE(CONFIG_THERMAL)) {
650 + ret = emc2305_set_tz(dev);
651 + if (ret != 0)
652 + return ret;
653 + }
654 +
655 + for (i = 0; i < data->pwm_num; i++)
656 + i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_MIN_DRIVE(i), data->pwm_min);
657 +
658 + return 0;
659 +}
660 +
661 +static int emc2305_remove(struct i2c_client *client)
662 +{
663 + struct device *dev = &client->dev;
664 +
665 + if (IS_REACHABLE(CONFIG_THERMAL))
666 + emc2305_unset_tz(dev);
667 + return 0;
668 +}
669 +
670 +static struct i2c_driver emc2305_driver = {
671 + .class = I2C_CLASS_HWMON,
672 + .driver = {
673 + .name = "emc2305",
674 + .of_match_table = emc2305_dt_ids,
675 + },
676 + .probe = emc2305_probe,
677 + .remove = emc2305_remove,
678 + .id_table = emc2305_ids,
679 + .address_list = emc2305_normal_i2c,
680 +};
681 +
682 +module_i2c_driver(emc2305_driver);
683 +
684 +MODULE_AUTHOR("Nvidia");
685 +MODULE_DESCRIPTION("Microchip EMC2305 fan controller driver");
686 +MODULE_LICENSE("GPL");