Lines Matching +full:reg +full:- +full:data
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * max6650.c - Part of lm_sensors, Linux kernel modules for hardware
18 * http://pdfserv.maxim-ic.com/en/ds/MAX6650-MAX6651.pdf
27 #include <linux/hwmon-sysfs.h>
91 /* Minimum and maximum values of the FAN-RPM */
95 #define DIV_FROM_REG(reg) (1 << ((reg) & 7)) argument
99 * Client data (each client gets its own)
130 .data = (void *)1
134 .data = (void *)4
143 * Useful range for dac is 0-180 for 12V fans and 0-76 for 5V fans. in dac_to_pwm()
146 return clamp_val(255 - (255 * dac) / DAC_LIMIT(v12), 0, 255); in dac_to_pwm()
153 return limit - (limit * pwm) / 255; in pwm_to_dac()
158 struct max6650_data *data = dev_get_drvdata(dev); in max6650_update_device() local
159 struct i2c_client *client = data->client; in max6650_update_device()
160 int reg, err = 0; in max6650_update_device() local
163 mutex_lock(&data->update_lock); in max6650_update_device()
165 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { in max6650_update_device()
166 for (i = 0; i < data->nr_fans; i++) { in max6650_update_device()
167 reg = i2c_smbus_read_byte_data(client, tach_reg[i]); in max6650_update_device()
168 if (reg < 0) { in max6650_update_device()
169 err = reg; in max6650_update_device()
172 data->tach[i] = reg; in max6650_update_device()
180 reg = i2c_smbus_read_byte_data(client, MAX6650_REG_ALARM); in max6650_update_device()
181 if (reg < 0) { in max6650_update_device()
182 err = reg; in max6650_update_device()
185 data->alarm |= reg; in max6650_update_device()
186 data->last_updated = jiffies; in max6650_update_device()
187 data->valid = true; in max6650_update_device()
191 mutex_unlock(&data->update_lock); in max6650_update_device()
193 data = ERR_PTR(err); in max6650_update_device()
194 return data; in max6650_update_device()
201 static int max6650_set_operating_mode(struct max6650_data *data, u8 mode) in max6650_set_operating_mode() argument
204 u8 config = data->config; in max6650_set_operating_mode()
211 result = i2c_smbus_write_byte_data(data->client, MAX6650_REG_CONFIG, in max6650_set_operating_mode()
216 data->config = config; in max6650_set_operating_mode()
262 static int max6650_set_target(struct max6650_data *data, unsigned long rpm) in max6650_set_target() argument
267 return max6650_set_operating_mode(data, MAX6650_CFG_MODE_OFF); in max6650_set_target()
275 * KTACH = [(fCLK x KSCALE) / (256 x FanSpeed)] - 1 in max6650_set_target()
278 kscale = DIV_FROM_REG(data->config); in max6650_set_target()
279 ktach = ((clock * kscale) / (256 * rpm / 60)) - 1; in max6650_set_target()
284 data->speed = ktach; in max6650_set_target()
286 return i2c_smbus_write_byte_data(data->client, MAX6650_REG_SPEED, in max6650_set_target()
287 data->speed); in max6650_set_target()
301 struct max6650_data *data = max6650_update_device(dev); in alarm_show() local
304 if (IS_ERR(data)) in alarm_show()
305 return PTR_ERR(data); in alarm_show()
307 alarm = data->alarm & attr->index; in alarm_show()
309 mutex_lock(&data->update_lock); in alarm_show()
310 data->alarm &= ~attr->index; in alarm_show()
311 data->valid = false; in alarm_show()
312 mutex_unlock(&data->update_lock); in alarm_show()
325 struct max6650_data *data = dev_get_drvdata(dev); in max6650_attrs_visible() local
335 if (!(data->alarm_en & to_sensor_dev_attr(devattr)->index)) in max6650_attrs_visible()
339 return a->mode; in max6650_attrs_visible()
358 static int max6650_init_client(struct max6650_data *data, in max6650_init_client() argument
361 struct device *dev = &client->dev; in max6650_init_client()
362 int reg; in max6650_init_client() local
368 if (of_property_read_u32(dev->of_node, "maxim,fan-microvolt", in max6650_init_client()
373 if (of_property_read_u32(dev->of_node, "maxim,fan-prescale", in max6650_init_client()
377 reg = i2c_smbus_read_byte_data(client, MAX6650_REG_CONFIG); in max6650_init_client()
378 if (reg < 0) { in max6650_init_client()
380 return reg; in max6650_init_client()
387 reg &= ~MAX6650_CFG_V12; in max6650_init_client()
390 reg |= MAX6650_CFG_V12; in max6650_init_client()
400 reg &= ~MAX6650_CFG_PRESCALER_MASK; in max6650_init_client()
403 reg = (reg & ~MAX6650_CFG_PRESCALER_MASK) in max6650_init_client()
407 reg = (reg & ~MAX6650_CFG_PRESCALER_MASK) in max6650_init_client()
411 reg = (reg & ~MAX6650_CFG_PRESCALER_MASK) in max6650_init_client()
415 reg = (reg & ~MAX6650_CFG_PRESCALER_MASK) in max6650_init_client()
423 (reg & MAX6650_CFG_V12) ? 12 : 5, in max6650_init_client()
424 1 << (reg & MAX6650_CFG_PRESCALER_MASK)); in max6650_init_client()
426 err = i2c_smbus_write_byte_data(client, MAX6650_REG_CONFIG, reg); in max6650_init_client()
431 data->config = reg; in max6650_init_client()
433 reg = i2c_smbus_read_byte_data(client, MAX6650_REG_SPEED); in max6650_init_client()
434 if (reg < 0) { in max6650_init_client()
436 return reg; in max6650_init_client()
438 data->speed = reg; in max6650_init_client()
440 reg = i2c_smbus_read_byte_data(client, MAX6650_REG_DAC); in max6650_init_client()
441 if (reg < 0) { in max6650_init_client()
443 return reg; in max6650_init_client()
445 data->dac = reg; in max6650_init_client()
447 reg = i2c_smbus_read_byte_data(client, MAX6650_REG_COUNT); in max6650_init_client()
448 if (reg < 0) { in max6650_init_client()
450 return reg; in max6650_init_client()
452 data->count = reg; in max6650_init_client()
454 reg = i2c_smbus_read_byte_data(client, MAX6650_REG_ALARM_EN); in max6650_init_client()
455 if (reg < 0) { in max6650_init_client()
457 return reg; in max6650_init_client()
459 data->alarm_en = reg; in max6650_init_client()
461 if (!of_property_read_u32(client->dev.of_node, "maxim,fan-target-rpm", in max6650_init_client()
463 max6650_set_target(data, target_rpm); in max6650_init_client()
464 max6650_set_operating_mode(data, MAX6650_CFG_MODE_CLOSED_LOOP); in max6650_init_client()
481 struct max6650_data *data = cdev->devdata; in max6650_get_cur_state() local
483 *state = data->cooling_dev_state; in max6650_get_cur_state()
491 struct max6650_data *data = cdev->devdata; in max6650_set_cur_state() local
492 struct i2c_client *client = data->client; in max6650_set_cur_state()
497 mutex_lock(&data->update_lock); in max6650_set_cur_state()
499 data->dac = pwm_to_dac(state, data->config & MAX6650_CFG_V12); in max6650_set_cur_state()
500 err = i2c_smbus_write_byte_data(client, MAX6650_REG_DAC, data->dac); in max6650_set_cur_state()
502 max6650_set_operating_mode(data, state ? in max6650_set_cur_state()
505 data->cooling_dev_state = state; in max6650_set_cur_state()
508 mutex_unlock(&data->update_lock); in max6650_set_cur_state()
522 struct max6650_data *data = max6650_update_device(dev); in max6650_read() local
525 if (IS_ERR(data)) in max6650_read()
526 return PTR_ERR(data); in max6650_read()
532 *val = dac_to_pwm(data->dac, in max6650_read()
533 data->config & MAX6650_CFG_V12); in max6650_read()
545 mode = (data->config & MAX6650_CFG_MODE_MASK) >> 4; in max6650_read()
546 *val = (4 - mode) & 3; /* {0 1 2 3} -> {0 3 2 1} */ in max6650_read()
549 return -EOPNOTSUPP; in max6650_read()
563 *val = DIV_ROUND_CLOSEST(data->tach[channel] * 120, in max6650_read()
564 DIV_FROM_REG(data->count)); in max6650_read()
567 *val = DIV_FROM_REG(data->count); in max6650_read()
575 *val = 60 * DIV_FROM_REG(data->config) * clock / in max6650_read()
576 (256 * (data->speed + 1)); in max6650_read()
579 *val = !!(data->alarm & MAX6650_ALRM_MIN); in max6650_read()
580 data->alarm &= ~MAX6650_ALRM_MIN; in max6650_read()
581 data->valid = false; in max6650_read()
584 *val = !!(data->alarm & MAX6650_ALRM_MAX); in max6650_read()
585 data->alarm &= ~MAX6650_ALRM_MAX; in max6650_read()
586 data->valid = false; in max6650_read()
589 *val = !!(data->alarm & MAX6650_ALRM_TACH); in max6650_read()
590 data->alarm &= ~MAX6650_ALRM_TACH; in max6650_read()
591 data->valid = false; in max6650_read()
594 return -EOPNOTSUPP; in max6650_read()
598 return -EOPNOTSUPP; in max6650_read()
613 struct max6650_data *data = dev_get_drvdata(dev); in max6650_write() local
615 u8 reg; in max6650_write() local
617 mutex_lock(&data->update_lock); in max6650_write()
623 reg = pwm_to_dac(clamp_val(val, 0, 255), in max6650_write()
624 data->config & MAX6650_CFG_V12); in max6650_write()
625 ret = i2c_smbus_write_byte_data(data->client, in max6650_write()
626 MAX6650_REG_DAC, reg); in max6650_write()
629 data->dac = reg; in max6650_write()
633 ret = -EINVAL; in max6650_write()
636 ret = max6650_set_operating_mode(data, in max6650_write()
640 ret = -EOPNOTSUPP; in max6650_write()
649 reg = 0; in max6650_write()
652 reg = 1; in max6650_write()
655 reg = 2; in max6650_write()
658 reg = 3; in max6650_write()
661 ret = -EINVAL; in max6650_write()
664 ret = i2c_smbus_write_byte_data(data->client, in max6650_write()
665 MAX6650_REG_COUNT, reg); in max6650_write()
668 data->count = reg; in max6650_write()
672 ret = -EINVAL; in max6650_write()
675 ret = max6650_set_target(data, val); in max6650_write()
678 ret = -EOPNOTSUPP; in max6650_write()
683 ret = -EOPNOTSUPP; in max6650_write()
688 mutex_unlock(&data->update_lock); in max6650_write()
696 const struct max6650_data *data = _data; in max6650_is_visible() local
698 if (channel && (channel >= data->nr_fans || type != hwmon_fan)) in max6650_is_visible()
710 if (data->alarm_en & MAX6650_ALRM_MIN) in max6650_is_visible()
714 if (data->alarm_en & MAX6650_ALRM_MAX) in max6650_is_visible()
718 if (data->alarm_en & MAX6650_ALRM_TACH) in max6650_is_visible()
765 struct device *dev = &client->dev; in max6650_probe()
766 struct max6650_data *data; in max6650_probe() local
770 data = devm_kzalloc(dev, sizeof(struct max6650_data), GFP_KERNEL); in max6650_probe()
771 if (!data) in max6650_probe()
772 return -ENOMEM; in max6650_probe()
774 data->client = client; in max6650_probe()
775 i2c_set_clientdata(client, data); in max6650_probe()
776 mutex_init(&data->update_lock); in max6650_probe()
778 data->nr_fans = (uintptr_t)i2c_get_match_data(client); in max6650_probe()
783 err = max6650_init_client(data, client); in max6650_probe()
788 client->name, data, in max6650_probe()
797 dev->of_node, client->name, in max6650_probe()
798 data, &max6650_cooling_ops); in max6650_probe()