File Index
Files and directories
Sign in
FILE

emc2305.c

File preview with syntax highlighting.

linux-6.19.14/drivers/hwmon/emc2305.c C
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 * Hardware monitoring driver for EMC2305 fan controller
  4 *
  5 * Copyright (C) 2022 Nvidia Technologies Ltd.
  6 */
  7
  8#include <linux/err.h>
  9#include <linux/hwmon.h>
 10#include <linux/i2c.h>
 11#include <linux/module.h>
 12#include <linux/platform_data/emc2305.h>
 13#include <linux/thermal.h>
 14#include <linux/pwm.h>
 15#include <linux/of_device.h>
 16#include <linux/util_macros.h>
 17
 18#define EMC2305_REG_DRIVE_FAIL_STATUS	0x27
 19#define EMC2305_REG_VENDOR		0xfe
 20#define EMC2305_FAN_MAX			0xff
 21#define EMC2305_FAN_MIN			0x00
 22#define EMC2305_FAN_MAX_STATE		10
 23#define EMC2305_DEVICE			0x34
 24#define EMC2305_VENDOR			0x5d
 25#define EMC2305_REG_PRODUCT_ID		0xfd
 26#define EMC2305_TACH_REGS_UNUSE_BITS	3
 27#define EMC2305_TACH_CNT_MULTIPLIER	0x02
 28#define EMC2305_TACH_RANGE_MIN		480
 29#define EMC2305_DEFAULT_OUTPUT		0x0
 30#define EMC2305_DEFAULT_POLARITY	0x0
 31#define EMC2305_REG_POLARITY		0x2a
 32#define EMC2305_REG_DRIVE_PWM_OUT	0x2b
 33#define EMC2305_OPEN_DRAIN		0x0
 34#define EMC2305_PUSH_PULL		0x1
 35
 36#define EMC2305_PWM_DUTY2STATE(duty, max_state, pwm_max) \
 37	DIV_ROUND_CLOSEST((duty) * (max_state), (pwm_max))
 38#define EMC2305_PWM_STATE2DUTY(state, max_state, pwm_max) \
 39	DIV_ROUND_CLOSEST((state) * (pwm_max), (max_state))
 40
 41/*
 42 * Factor by equations [2] and [3] from data sheet; valid for fans where the number of edges
 43 * equal (poles * 2 + 1).
 44 */
 45#define EMC2305_RPM_FACTOR		3932160
 46
 47#define EMC2305_REG_FAN_DRIVE(n)	(0x30 + 0x10 * (n))
 48#define EMC2305_REG_FAN_MIN_DRIVE(n)	(0x38 + 0x10 * (n))
 49#define EMC2305_REG_FAN_TACH(n)		(0x3e + 0x10 * (n))
 50
 51/* Supported base PWM frequencies */
 52static const unsigned int base_freq_table[] = { 2441, 4882, 19530, 26000 };
 53
 54enum emc230x_product_id {
 55	EMC2305 = 0x34,
 56	EMC2303 = 0x35,
 57	EMC2302 = 0x36,
 58	EMC2301 = 0x37,
 59};
 60
 61static const struct i2c_device_id emc2305_ids[] = {
 62	{ "emc2305" },
 63	{ "emc2303" },
 64	{ "emc2302" },
 65	{ "emc2301" },
 66	{ }
 67};
 68MODULE_DEVICE_TABLE(i2c, emc2305_ids);
 69
 70/**
 71 * struct emc2305_cdev_data - device-specific cooling device state
 72 * @cdev: cooling device
 73 * @cur_state: cooling current state
 74 * @last_hwmon_state: last cooling state updated by hwmon subsystem
 75 * @last_thermal_state: last cooling state updated by thermal subsystem
 76 *
 77 * The 'last_hwmon_state' and 'last_thermal_state' fields are provided to support fan low limit
 78 * speed feature. The purpose of this feature is to provides ability to limit fan speed
 79 * according to some system wise considerations, like absence of some replaceable units (PSU or
 80 * line cards), high system ambient temperature, unreliable transceivers temperature sensing or
 81 * some other factors which indirectly impacts system's airflow
 82 * Fan low limit feature is supported through 'hwmon' interface: 'hwmon' 'pwm' attribute is
 83 * used for setting low limit for fan speed in case 'thermal' subsystem is configured in
 84 * kernel. In this case setting fan speed through 'hwmon' will never let the 'thermal'
 85 * subsystem to select a lower duty cycle than the duty cycle selected with the 'pwm'
 86 * attribute.
 87 * From other side, fan speed is to be updated in hardware through 'pwm' only in case the
 88 * requested fan speed is above last speed set by 'thermal' subsystem, otherwise requested fan
 89 * speed will be just stored with no PWM update.
 90 */
 91struct emc2305_cdev_data {
 92	struct thermal_cooling_device *cdev;
 93	unsigned int cur_state;
 94	unsigned long last_hwmon_state;
 95	unsigned long last_thermal_state;
 96};
 97
 98/**
 99 * struct emc2305_data - device-specific data
100 * @client: i2c client
101 * @hwmon_dev: hwmon device
102 * @max_state: maximum cooling state of the cooling device
103 * @pwm_num: number of PWM channels
104 * @pwm_output_mask: PWM output mask
105 * @pwm_polarity_mask: PWM polarity mask
106 * @pwm_separate: separate PWM settings for every channel
107 * @pwm_min: array of minimum PWM per channel
108 * @pwm_freq: array of PWM frequency per channel
109 * @cdev_data: array of cooling devices data
110 */
111struct emc2305_data {
112	struct i2c_client *client;
113	struct device *hwmon_dev;
114	u8 max_state;
115	u8 pwm_num;
116	u8 pwm_output_mask;
117	u8 pwm_polarity_mask;
118	bool pwm_separate;
119	u8 pwm_min[EMC2305_PWM_MAX];
120	u16 pwm_freq[EMC2305_PWM_MAX];
121	struct emc2305_cdev_data cdev_data[EMC2305_PWM_MAX];
122};
123
124static char *emc2305_fan_name[] = {
125	"emc2305_fan",
126	"emc2305_fan1",
127	"emc2305_fan2",
128	"emc2305_fan3",
129	"emc2305_fan4",
130	"emc2305_fan5",
131};
132
133static int emc2305_get_max_channel(const struct emc2305_data *data)
134{
135	return data->pwm_num;
136}
137
138static int emc2305_get_cdev_idx(struct thermal_cooling_device *cdev)
139{
140	struct emc2305_data *data = cdev->devdata;
141	size_t len = strlen(cdev->type);
142	int ret;
143
144	if (len <= 0)
145		return -EINVAL;
146
147	/*
148	 * Returns index of cooling device 0..4 in case of separate PWM setting.
149	 * Zero index is used in case of one common PWM setting.
150	 * If the mode is not set as pwm_separate, all PWMs are to be bound
151	 * to the common thermal zone and should work at the same speed
152	 * to perform cooling for the same thermal junction.
153	 * Otherwise, return specific channel that will be used in bound
154	 * related PWM to the thermal zone.
155	 */
156	if (!data->pwm_separate)
157		return 0;
158
159	ret = cdev->type[len - 1];
160	switch (ret) {
161	case '1' ... '5':
162		return ret - '1';
163	default:
164		break;
165	}
166	return -EINVAL;
167}
168
169static int emc2305_get_cur_state(struct thermal_cooling_device *cdev, unsigned long *state)
170{
171	int cdev_idx;
172	struct emc2305_data *data = cdev->devdata;
173
174	cdev_idx = emc2305_get_cdev_idx(cdev);
175	if (cdev_idx < 0)
176		return cdev_idx;
177
178	*state = data->cdev_data[cdev_idx].cur_state;
179	return 0;
180}
181
182static int emc2305_get_max_state(struct thermal_cooling_device *cdev, unsigned long *state)
183{
184	struct emc2305_data *data = cdev->devdata;
185	*state = data->max_state;
186	return 0;
187}
188
189static int __emc2305_set_cur_state(struct emc2305_data *data, int cdev_idx, unsigned long state)
190{
191	int ret;
192	struct i2c_client *client = data->client;
193	u8 val, i;
194
195	state = max_t(unsigned long, state, data->cdev_data[cdev_idx].last_hwmon_state);
196
197	val = EMC2305_PWM_STATE2DUTY(state, data->max_state, EMC2305_FAN_MAX);
198
199	data->cdev_data[cdev_idx].cur_state = state;
200	if (data->pwm_separate) {
201		ret = i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(cdev_idx), val);
202		if (ret < 0)
203			return ret;
204	} else {
205		/*
206		 * Set the same PWM value in all channels
207		 * if common PWM channel is used.
208		 */
209		for (i = 0; i < data->pwm_num; i++) {
210			ret = i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(i), val);
211			if (ret < 0)
212				return ret;
213		}
214	}
215
216	return 0;
217}
218
219static int emc2305_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
220{
221	int cdev_idx, ret;
222	struct emc2305_data *data = cdev->devdata;
223
224	if (state > data->max_state)
225		return -EINVAL;
226
227	cdev_idx =  emc2305_get_cdev_idx(cdev);
228	if (cdev_idx < 0)
229		return cdev_idx;
230
231	/* Save thermal state. */
232	data->cdev_data[cdev_idx].last_thermal_state = state;
233	ret = __emc2305_set_cur_state(data, cdev_idx, state);
234	if (ret < 0)
235		return ret;
236
237	return 0;
238}
239
240static const struct thermal_cooling_device_ops emc2305_cooling_ops = {
241	.get_max_state = emc2305_get_max_state,
242	.get_cur_state = emc2305_get_cur_state,
243	.set_cur_state = emc2305_set_cur_state,
244};
245
246static int emc2305_show_fault(struct device *dev, int channel)
247{
248	struct emc2305_data *data = dev_get_drvdata(dev);
249	struct i2c_client *client = data->client;
250	int status_reg;
251
252	status_reg = i2c_smbus_read_byte_data(client, EMC2305_REG_DRIVE_FAIL_STATUS);
253	if (status_reg < 0)
254		return status_reg;
255
256	return status_reg & (1 << channel) ? 1 : 0;
257}
258
259static int emc2305_show_fan(struct device *dev, int channel)
260{
261	struct emc2305_data *data = dev_get_drvdata(dev);
262	struct i2c_client *client = data->client;
263	int ret;
264
265	ret = i2c_smbus_read_word_swapped(client, EMC2305_REG_FAN_TACH(channel));
266	if (ret <= 0)
267		return ret;
268
269	ret = ret >> EMC2305_TACH_REGS_UNUSE_BITS;
270	ret = EMC2305_RPM_FACTOR / ret;
271	if (ret <= EMC2305_TACH_RANGE_MIN)
272		return 0;
273
274	return ret * EMC2305_TACH_CNT_MULTIPLIER;
275}
276
277static int emc2305_show_pwm(struct device *dev, int channel)
278{
279	struct emc2305_data *data = dev_get_drvdata(dev);
280	struct i2c_client *client = data->client;
281
282	return i2c_smbus_read_byte_data(client, EMC2305_REG_FAN_DRIVE(channel));
283}
284
285static int emc2305_set_pwm(struct device *dev, long val, int channel)
286{
287	struct emc2305_data *data = dev_get_drvdata(dev);
288	struct i2c_client *client = data->client;
289	int ret;
290
291	if (val < data->pwm_min[channel] || val > EMC2305_FAN_MAX)
292		return -EINVAL;
293
294	ret = i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(channel), val);
295	if (ret < 0)
296		return ret;
297	data->cdev_data[channel].cur_state = EMC2305_PWM_DUTY2STATE(val, data->max_state,
298								    EMC2305_FAN_MAX);
299	return 0;
300}
301
302static int emc2305_set_single_tz(struct device *dev, struct device_node *fan_node, int idx)
303{
304	struct emc2305_data *data = dev_get_drvdata(dev);
305	long pwm;
306	int i, cdev_idx, ret;
307
308	cdev_idx = (idx) ? idx - 1 : 0;
309	pwm = data->pwm_min[cdev_idx];
310
311	data->cdev_data[cdev_idx].cdev =
312		devm_thermal_of_cooling_device_register(dev, fan_node,
313							emc2305_fan_name[idx], data,
314							&emc2305_cooling_ops);
315
316	if (IS_ERR(data->cdev_data[cdev_idx].cdev)) {
317		dev_err(dev, "Failed to register cooling device %s\n", emc2305_fan_name[idx]);
318		return PTR_ERR(data->cdev_data[cdev_idx].cdev);
319	}
320
321	if (data->cdev_data[cdev_idx].cur_state > 0)
322		/* Update pwm when temperature is above trips */
323		pwm = EMC2305_PWM_STATE2DUTY(data->cdev_data[cdev_idx].cur_state,
324					     data->max_state, EMC2305_FAN_MAX);
325
326	/* Set minimal PWM speed. */
327	if (data->pwm_separate) {
328		ret = emc2305_set_pwm(dev, pwm, cdev_idx);
329		if (ret < 0)
330			return ret;
331	} else {
332		for (i = 0; i < data->pwm_num; i++) {
333			ret = emc2305_set_pwm(dev, pwm, i);
334			if (ret < 0)
335				return ret;
336		}
337	}
338	data->cdev_data[cdev_idx].cur_state =
339		EMC2305_PWM_DUTY2STATE(pwm, data->max_state,
340				       EMC2305_FAN_MAX);
341	data->cdev_data[cdev_idx].last_hwmon_state =
342		EMC2305_PWM_DUTY2STATE(pwm, data->max_state,
343				       EMC2305_FAN_MAX);
344	return 0;
345}
346
347static int emc2305_set_tz(struct device *dev)
348{
349	struct emc2305_data *data = dev_get_drvdata(dev);
350	int i, ret;
351
352	if (!data->pwm_separate)
353		return emc2305_set_single_tz(dev, dev->of_node, 0);
354
355	for (i = 0; i < data->pwm_num; i++) {
356		ret = emc2305_set_single_tz(dev, dev->of_node, i + 1);
357		if (ret)
358			return ret;
359	}
360	return 0;
361}
362
363static umode_t
364emc2305_is_visible(const void *data, enum hwmon_sensor_types type, u32 attr, int channel)
365{
366	int max_channel = emc2305_get_max_channel(data);
367
368	/* Don't show channels which are not physically connected. */
369	if (channel >= max_channel)
370		return 0;
371	switch (type) {
372	case hwmon_fan:
373		switch (attr) {
374		case hwmon_fan_input:
375			return 0444;
376		case hwmon_fan_fault:
377			return 0444;
378		default:
379			break;
380		}
381		break;
382	case hwmon_pwm:
383		switch (attr) {
384		case hwmon_pwm_input:
385			return 0644;
386		default:
387			break;
388		}
389		break;
390	default:
391		break;
392	}
393
394	return 0;
395};
396
397static int
398emc2305_write(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long val)
399{
400	struct emc2305_data *data = dev_get_drvdata(dev);
401	int cdev_idx;
402
403	switch (type) {
404	case hwmon_pwm:
405		switch (attr) {
406		case hwmon_pwm_input:
407			/* If thermal is configured - handle PWM limit setting. */
408			if (IS_REACHABLE(CONFIG_THERMAL)) {
409				if (data->pwm_separate)
410					cdev_idx = channel;
411				else
412					cdev_idx = 0;
413				data->cdev_data[cdev_idx].last_hwmon_state =
414					EMC2305_PWM_DUTY2STATE(val, data->max_state,
415							       EMC2305_FAN_MAX);
416				/*
417				 * Update PWM only in case requested state is not less than the
418				 * last thermal state.
419				 */
420				if (data->cdev_data[cdev_idx].last_hwmon_state >=
421				    data->cdev_data[cdev_idx].last_thermal_state)
422					return __emc2305_set_cur_state(data, cdev_idx,
423							data->cdev_data[cdev_idx].last_hwmon_state);
424				return 0;
425			}
426			return emc2305_set_pwm(dev, val, channel);
427		default:
428			break;
429		}
430		break;
431	default:
432		break;
433	}
434
435	return -EOPNOTSUPP;
436};
437
438static int
439emc2305_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long *val)
440{
441	int ret;
442
443	switch (type) {
444	case hwmon_fan:
445		switch (attr) {
446		case hwmon_fan_input:
447			ret = emc2305_show_fan(dev, channel);
448			if (ret < 0)
449				return ret;
450			*val = ret;
451			return 0;
452		case hwmon_fan_fault:
453			ret = emc2305_show_fault(dev, channel);
454			if (ret < 0)
455				return ret;
456			*val = ret;
457			return 0;
458		default:
459			break;
460		}
461		break;
462	case hwmon_pwm:
463		switch (attr) {
464		case hwmon_pwm_input:
465			ret = emc2305_show_pwm(dev, channel);
466			if (ret < 0)
467				return ret;
468			*val = ret;
469			return 0;
470		default:
471			break;
472		}
473		break;
474	default:
475		break;
476	}
477
478	return -EOPNOTSUPP;
479};
480
481static const struct hwmon_ops emc2305_ops = {
482	.is_visible = emc2305_is_visible,
483	.read = emc2305_read,
484	.write = emc2305_write,
485};
486
487static const struct hwmon_channel_info * const emc2305_info[] = {
488	HWMON_CHANNEL_INFO(fan,
489			   HWMON_F_INPUT | HWMON_F_FAULT,
490			   HWMON_F_INPUT | HWMON_F_FAULT,
491			   HWMON_F_INPUT | HWMON_F_FAULT,
492			   HWMON_F_INPUT | HWMON_F_FAULT,
493			   HWMON_F_INPUT | HWMON_F_FAULT),
494	HWMON_CHANNEL_INFO(pwm,
495			   HWMON_PWM_INPUT,
496			   HWMON_PWM_INPUT,
497			   HWMON_PWM_INPUT,
498			   HWMON_PWM_INPUT,
499			   HWMON_PWM_INPUT),
500	NULL
501};
502
503static const struct hwmon_chip_info emc2305_chip_info = {
504	.ops = &emc2305_ops,
505	.info = emc2305_info,
506};
507
508static int emc2305_identify(struct device *dev)
509{
510	struct i2c_client *client = to_i2c_client(dev);
511	struct emc2305_data *data = i2c_get_clientdata(client);
512	int ret;
513
514	ret = i2c_smbus_read_byte_data(client, EMC2305_REG_PRODUCT_ID);
515	if (ret < 0)
516		return ret;
517
518	switch (ret) {
519	case EMC2305:
520		data->pwm_num = 5;
521		break;
522	case EMC2303:
523		data->pwm_num = 3;
524		break;
525	case EMC2302:
526		data->pwm_num = 2;
527		break;
528	case EMC2301:
529		data->pwm_num = 1;
530		break;
531	default:
532		return -ENODEV;
533	}
534
535	return 0;
536}
537
538static int emc2305_of_parse_pwm_child(struct device *dev,
539				      struct device_node *child,
540				      struct emc2305_data *data)
541{	u32 ch;
542	int ret;
543	struct of_phandle_args args;
544
545	ret = of_property_read_u32(child, "reg", &ch);
546	if (ret) {
547		dev_err(dev, "missing reg property of %pOFn\n", child);
548		return ret;
549	}
550
551	ret = of_parse_phandle_with_args(child, "pwms", "#pwm-cells", 0, &args);
552
553	if (ret)
554		return ret;
555
556	if (args.args_count > 0) {
557		data->pwm_freq[ch] = find_closest(args.args[0], base_freq_table,
558						  ARRAY_SIZE(base_freq_table));
559	} else {
560		data->pwm_freq[ch] = base_freq_table[3];
561	}
562
563	if (args.args_count > 1) {
564		if (args.args[1] == PWM_POLARITY_NORMAL || args.args[1] == PWM_POLARITY_INVERSED)
565			data->pwm_polarity_mask |= args.args[1] << ch;
566		else
567			dev_err(dev, "Wrong PWM polarity config provided: %d\n", args.args[0]);
568	} else {
569		data->pwm_polarity_mask |= PWM_POLARITY_NORMAL << ch;
570	}
571
572	if (args.args_count > 2) {
573		if (args.args[2] == EMC2305_PUSH_PULL || args.args[2] <= EMC2305_OPEN_DRAIN)
574			data->pwm_output_mask |= args.args[2] << ch;
575		else
576			dev_err(dev, "Wrong PWM output config provided: %d\n", args.args[1]);
577	} else {
578		data->pwm_output_mask |= EMC2305_OPEN_DRAIN << ch;
579	}
580
581	of_node_put(args.np);
582	return 0;
583}
584
585static int emc2305_probe_childs_from_dt(struct device *dev)
586{
587	struct emc2305_data *data = dev_get_drvdata(dev);
588	struct device_node *child;
589	int ret, count = 0;
590
591	data->pwm_output_mask = 0x0;
592	data->pwm_polarity_mask = 0x0;
593
594	for_each_child_of_node(dev->of_node, child) {
595		if (of_property_present(child, "reg")) {
596			ret = emc2305_of_parse_pwm_child(dev, child, data);
597			if (ret)
598				continue;
599			count++;
600		}
601	}
602	return count;
603}
604
605static int emc2305_probe(struct i2c_client *client)
606{
607	struct i2c_adapter *adapter = client->adapter;
608	struct device *dev = &client->dev;
609	struct device_node *child;
610	struct emc2305_data *data;
611	struct emc2305_platform_data *pdata;
612	int vendor;
613	int ret;
614	int i;
615	int pwm_childs;
616
617	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
618		return -ENODEV;
619
620	vendor = i2c_smbus_read_byte_data(client, EMC2305_REG_VENDOR);
621	if (vendor != EMC2305_VENDOR)
622		return -ENODEV;
623
624	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
625	if (!data)
626		return -ENOMEM;
627
628	i2c_set_clientdata(client, data);
629	data->client = client;
630
631	ret = emc2305_identify(dev);
632	if (ret)
633		return ret;
634
635	pwm_childs = emc2305_probe_childs_from_dt(dev);
636
637	pdata = dev_get_platdata(&client->dev);
638
639	if (!pwm_childs) {
640		if (pdata) {
641			if (!pdata->max_state || pdata->max_state > EMC2305_FAN_MAX_STATE)
642				return -EINVAL;
643			data->max_state = pdata->max_state;
644			/*
645			 * Validate a number of active PWM channels. Note that
646			 * configured number can be less than the actual maximum
647			 * supported by the device.
648			 */
649			if (!pdata->pwm_num || pdata->pwm_num > EMC2305_PWM_MAX)
650				return -EINVAL;
651			data->pwm_num = pdata->pwm_num;
652			data->pwm_output_mask = pdata->pwm_output_mask;
653			data->pwm_polarity_mask = pdata->pwm_polarity_mask;
654			data->pwm_separate = pdata->pwm_separate;
655			for (i = 0; i < EMC2305_PWM_MAX; i++) {
656				data->pwm_min[i] = pdata->pwm_min[i];
657				data->pwm_freq[i] = pdata->pwm_freq[i];
658			}
659		} else {
660			data->max_state = EMC2305_FAN_MAX_STATE;
661			data->pwm_separate = false;
662			data->pwm_output_mask = EMC2305_DEFAULT_OUTPUT;
663			data->pwm_polarity_mask = EMC2305_DEFAULT_POLARITY;
664			for (i = 0; i < EMC2305_PWM_MAX; i++) {
665				data->pwm_min[i] = EMC2305_FAN_MIN;
666				data->pwm_freq[i] = base_freq_table[3];
667			}
668		}
669	} else {
670		data->max_state = EMC2305_FAN_MAX_STATE;
671		data->pwm_separate = false;
672		for (i = 0; i < EMC2305_PWM_MAX; i++)
673			data->pwm_min[i] = EMC2305_FAN_MIN;
674	}
675
676	data->hwmon_dev = devm_hwmon_device_register_with_info(dev, "emc2305", data,
677							       &emc2305_chip_info, NULL);
678	if (IS_ERR(data->hwmon_dev))
679		return PTR_ERR(data->hwmon_dev);
680
681	if (IS_REACHABLE(CONFIG_THERMAL)) {
682		/* Parse and check for the available PWM child nodes */
683		if (pwm_childs > 0) {
684			i = 0;
685			for_each_child_of_node(dev->of_node, child) {
686				ret = emc2305_set_single_tz(dev, child, i);
687				if (ret != 0) {
688					of_node_put(child);
689					return ret;
690				}
691				i++;
692			}
693		} else {
694			ret = emc2305_set_tz(dev);
695			if (ret != 0)
696				return ret;
697		}
698	}
699
700	ret = i2c_smbus_write_byte_data(client, EMC2305_REG_DRIVE_PWM_OUT,
701					data->pwm_output_mask);
702	if (ret < 0)
703		dev_err(dev, "Failed to configure pwm output, using default\n");
704
705	ret = i2c_smbus_write_byte_data(client, EMC2305_REG_POLARITY,
706					data->pwm_polarity_mask);
707	if (ret < 0)
708		dev_err(dev, "Failed to configure pwm polarity, using default\n");
709
710	for (i = 0; i < data->pwm_num; i++) {
711		ret = i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_MIN_DRIVE(i),
712						data->pwm_min[i]);
713		if (ret < 0)
714			return ret;
715	}
716
717	return 0;
718}
719
720static const struct of_device_id of_emc2305_match_table[] = {
721	{ .compatible = "microchip,emc2305", },
722	{},
723};
724MODULE_DEVICE_TABLE(of, of_emc2305_match_table);
725
726static struct i2c_driver emc2305_driver = {
727	.driver = {
728		.name = "emc2305",
729		.of_match_table = of_emc2305_match_table,
730	},
731	.probe = emc2305_probe,
732	.id_table = emc2305_ids,
733};
734
735module_i2c_driver(emc2305_driver);
736
737MODULE_AUTHOR("Nvidia");
738MODULE_DESCRIPTION("Microchip EMC2305 fan controller driver");
739MODULE_LICENSE("GPL");
~/ Read-only file preview.