Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * max6650.c - Part of lm_sensors, Linux kernel modules for hardware
0004  *             monitoring.
0005  *
0006  * (C) 2007 by Hans J. Koch <hjk@hansjkoch.de>
0007  *
0008  * based on code written by John Morris <john.morris@spirentcom.com>
0009  * Copyright (c) 2003 Spirent Communications
0010  * and Claus Gindhart <claus.gindhart@kontron.com>
0011  *
0012  * This module has only been tested with the MAX6650 chip. It should
0013  * also work with the MAX6651. It does not distinguish max6650 and max6651
0014  * chips.
0015  *
0016  * The datasheet was last seen at:
0017  *
0018  *        http://pdfserv.maxim-ic.com/en/ds/MAX6650-MAX6651.pdf
0019  */
0020 
0021 #include <linux/module.h>
0022 #include <linux/init.h>
0023 #include <linux/slab.h>
0024 #include <linux/jiffies.h>
0025 #include <linux/i2c.h>
0026 #include <linux/hwmon.h>
0027 #include <linux/hwmon-sysfs.h>
0028 #include <linux/err.h>
0029 #include <linux/of_device.h>
0030 #include <linux/thermal.h>
0031 
0032 /*
0033  * Insmod parameters
0034  */
0035 
0036 /* fan_voltage: 5=5V fan, 12=12V fan, 0=don't change */
0037 static int fan_voltage;
0038 /* prescaler: Possible values are 1, 2, 4, 8, 16 or 0 for don't change */
0039 static int prescaler;
0040 /* clock: The clock frequency of the chip (max6651 can be clocked externally) */
0041 static int clock = 254000;
0042 
0043 module_param(fan_voltage, int, 0444);
0044 module_param(prescaler, int, 0444);
0045 module_param(clock, int, 0444);
0046 
0047 /*
0048  * MAX 6650/6651 registers
0049  */
0050 
0051 #define MAX6650_REG_SPEED   0x00
0052 #define MAX6650_REG_CONFIG  0x02
0053 #define MAX6650_REG_GPIO_DEF    0x04
0054 #define MAX6650_REG_DAC     0x06
0055 #define MAX6650_REG_ALARM_EN    0x08
0056 #define MAX6650_REG_ALARM   0x0A
0057 #define MAX6650_REG_TACH0   0x0C
0058 #define MAX6650_REG_TACH1   0x0E
0059 #define MAX6650_REG_TACH2   0x10
0060 #define MAX6650_REG_TACH3   0x12
0061 #define MAX6650_REG_GPIO_STAT   0x14
0062 #define MAX6650_REG_COUNT   0x16
0063 
0064 /*
0065  * Config register bits
0066  */
0067 
0068 #define MAX6650_CFG_V12         0x08
0069 #define MAX6650_CFG_PRESCALER_MASK  0x07
0070 #define MAX6650_CFG_PRESCALER_2     0x01
0071 #define MAX6650_CFG_PRESCALER_4     0x02
0072 #define MAX6650_CFG_PRESCALER_8     0x03
0073 #define MAX6650_CFG_PRESCALER_16    0x04
0074 #define MAX6650_CFG_MODE_MASK       0x30
0075 #define MAX6650_CFG_MODE_ON     0x00
0076 #define MAX6650_CFG_MODE_OFF        0x10
0077 #define MAX6650_CFG_MODE_CLOSED_LOOP    0x20
0078 #define MAX6650_CFG_MODE_OPEN_LOOP  0x30
0079 #define MAX6650_COUNT_MASK      0x03
0080 
0081 /*
0082  * Alarm status register bits
0083  */
0084 
0085 #define MAX6650_ALRM_MAX    0x01
0086 #define MAX6650_ALRM_MIN    0x02
0087 #define MAX6650_ALRM_TACH   0x04
0088 #define MAX6650_ALRM_GPIO1  0x08
0089 #define MAX6650_ALRM_GPIO2  0x10
0090 
0091 /* Minimum and maximum values of the FAN-RPM */
0092 #define FAN_RPM_MIN 240
0093 #define FAN_RPM_MAX 30000
0094 
0095 #define DIV_FROM_REG(reg)   (1 << ((reg) & 7))
0096 #define DAC_LIMIT(v12)      ((v12) ? 180 : 76)
0097 
0098 /*
0099  * Client data (each client gets its own)
0100  */
0101 
0102 struct max6650_data {
0103     struct i2c_client *client;
0104     struct mutex update_lock; /* protect alarm register updates */
0105     int nr_fans;
0106     bool valid; /* false until following fields are valid */
0107     unsigned long last_updated; /* in jiffies */
0108 
0109     /* register values */
0110     u8 speed;
0111     u8 config;
0112     u8 tach[4];
0113     u8 count;
0114     u8 dac;
0115     u8 alarm;
0116     u8 alarm_en;
0117     unsigned long cooling_dev_state;
0118 };
0119 
0120 static const u8 tach_reg[] = {
0121     MAX6650_REG_TACH0,
0122     MAX6650_REG_TACH1,
0123     MAX6650_REG_TACH2,
0124     MAX6650_REG_TACH3,
0125 };
0126 
0127 static const struct of_device_id __maybe_unused max6650_dt_match[] = {
0128     {
0129         .compatible = "maxim,max6650",
0130         .data = (void *)1
0131     },
0132     {
0133         .compatible = "maxim,max6651",
0134         .data = (void *)4
0135     },
0136     { },
0137 };
0138 MODULE_DEVICE_TABLE(of, max6650_dt_match);
0139 
0140 static int dac_to_pwm(int dac, bool v12)
0141 {
0142     /*
0143      * Useful range for dac is 0-180 for 12V fans and 0-76 for 5V fans.
0144      * Lower DAC values mean higher speeds.
0145      */
0146     return clamp_val(255 - (255 * dac) / DAC_LIMIT(v12), 0, 255);
0147 }
0148 
0149 static u8 pwm_to_dac(unsigned int pwm, bool v12)
0150 {
0151     int limit = DAC_LIMIT(v12);
0152 
0153     return limit - (limit * pwm) / 255;
0154 }
0155 
0156 static struct max6650_data *max6650_update_device(struct device *dev)
0157 {
0158     struct max6650_data *data = dev_get_drvdata(dev);
0159     struct i2c_client *client = data->client;
0160     int reg, err = 0;
0161     int i;
0162 
0163     mutex_lock(&data->update_lock);
0164 
0165     if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
0166         for (i = 0; i < data->nr_fans; i++) {
0167             reg = i2c_smbus_read_byte_data(client, tach_reg[i]);
0168             if (reg < 0) {
0169                 err = reg;
0170                 goto error;
0171             }
0172             data->tach[i] = reg;
0173         }
0174 
0175         /*
0176          * Alarms are cleared on read in case the condition that
0177          * caused the alarm is removed. Keep the value latched here
0178          * for providing the register through different alarm files.
0179          */
0180         reg = i2c_smbus_read_byte_data(client, MAX6650_REG_ALARM);
0181         if (reg < 0) {
0182             err = reg;
0183             goto error;
0184         }
0185         data->alarm |= reg;
0186         data->last_updated = jiffies;
0187         data->valid = true;
0188     }
0189 
0190 error:
0191     mutex_unlock(&data->update_lock);
0192     if (err)
0193         data = ERR_PTR(err);
0194     return data;
0195 }
0196 
0197 /*
0198  * Change the operating mode of the chip (if needed).
0199  * mode is one of the MAX6650_CFG_MODE_* values.
0200  */
0201 static int max6650_set_operating_mode(struct max6650_data *data, u8 mode)
0202 {
0203     int result;
0204     u8 config = data->config;
0205 
0206     if (mode == (config & MAX6650_CFG_MODE_MASK))
0207         return 0;
0208 
0209     config = (config & ~MAX6650_CFG_MODE_MASK) | mode;
0210 
0211     result = i2c_smbus_write_byte_data(data->client, MAX6650_REG_CONFIG,
0212                        config);
0213     if (result < 0)
0214         return result;
0215 
0216     data->config = config;
0217 
0218     return 0;
0219 }
0220 
0221 /*
0222  * Set the fan speed to the specified RPM (or read back the RPM setting).
0223  * This works in closed loop mode only. Use pwm1 for open loop speed setting.
0224  *
0225  * The MAX6650/1 will automatically control fan speed when in closed loop
0226  * mode.
0227  *
0228  * Assumptions:
0229  *
0230  * 1) The MAX6650/1 internal 254kHz clock frequency is set correctly. Use
0231  *    the clock module parameter if you need to fine tune this.
0232  *
0233  * 2) The prescaler (low three bits of the config register) has already
0234  *    been set to an appropriate value. Use the prescaler module parameter
0235  *    if your BIOS doesn't initialize the chip properly.
0236  *
0237  * The relevant equations are given on pages 21 and 22 of the datasheet.
0238  *
0239  * From the datasheet, the relevant equation when in regulation is:
0240  *
0241  *    [fCLK / (128 x (KTACH + 1))] = 2 x FanSpeed / KSCALE
0242  *
0243  * where:
0244  *
0245  *    fCLK is the oscillator frequency (either the 254kHz internal
0246  *         oscillator or the externally applied clock)
0247  *
0248  *    KTACH is the value in the speed register
0249  *
0250  *    FanSpeed is the speed of the fan in rps
0251  *
0252  *    KSCALE is the prescaler value (1, 2, 4, 8, or 16)
0253  *
0254  * When reading, we need to solve for FanSpeed. When writing, we need to
0255  * solve for KTACH.
0256  *
0257  * Note: this tachometer is completely separate from the tachometers
0258  * used to measure the fan speeds. Only one fan's speed (fan1) is
0259  * controlled.
0260  */
0261 
0262 static int max6650_set_target(struct max6650_data *data, unsigned long rpm)
0263 {
0264     int kscale, ktach;
0265 
0266     if (rpm == 0)
0267         return max6650_set_operating_mode(data, MAX6650_CFG_MODE_OFF);
0268 
0269     rpm = clamp_val(rpm, FAN_RPM_MIN, FAN_RPM_MAX);
0270 
0271     /*
0272      * Divide the required speed by 60 to get from rpm to rps, then
0273      * use the datasheet equation:
0274      *
0275      *     KTACH = [(fCLK x KSCALE) / (256 x FanSpeed)] - 1
0276      */
0277 
0278     kscale = DIV_FROM_REG(data->config);
0279     ktach = ((clock * kscale) / (256 * rpm / 60)) - 1;
0280     if (ktach < 0)
0281         ktach = 0;
0282     if (ktach > 255)
0283         ktach = 255;
0284     data->speed = ktach;
0285 
0286     return i2c_smbus_write_byte_data(data->client, MAX6650_REG_SPEED,
0287                      data->speed);
0288 }
0289 
0290 /*
0291  * Get gpio alarm status:
0292  * Possible values:
0293  * 0 = no alarm
0294  * 1 = alarm
0295  */
0296 
0297 static ssize_t alarm_show(struct device *dev,
0298               struct device_attribute *devattr, char *buf)
0299 {
0300     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0301     struct max6650_data *data = max6650_update_device(dev);
0302     bool alarm;
0303 
0304     if (IS_ERR(data))
0305         return PTR_ERR(data);
0306 
0307     alarm = data->alarm & attr->index;
0308     if (alarm) {
0309         mutex_lock(&data->update_lock);
0310         data->alarm &= ~attr->index;
0311         data->valid = false;
0312         mutex_unlock(&data->update_lock);
0313     }
0314 
0315     return sprintf(buf, "%d\n", alarm);
0316 }
0317 
0318 static SENSOR_DEVICE_ATTR_RO(gpio1_alarm, alarm, MAX6650_ALRM_GPIO1);
0319 static SENSOR_DEVICE_ATTR_RO(gpio2_alarm, alarm, MAX6650_ALRM_GPIO2);
0320 
0321 static umode_t max6650_attrs_visible(struct kobject *kobj, struct attribute *a,
0322                      int n)
0323 {
0324     struct device *dev = kobj_to_dev(kobj);
0325     struct max6650_data *data = dev_get_drvdata(dev);
0326     struct device_attribute *devattr;
0327 
0328     /*
0329      * Hide the alarms that have not been enabled by the firmware
0330      */
0331 
0332     devattr = container_of(a, struct device_attribute, attr);
0333     if (devattr == &sensor_dev_attr_gpio1_alarm.dev_attr ||
0334         devattr == &sensor_dev_attr_gpio2_alarm.dev_attr) {
0335         if (!(data->alarm_en & to_sensor_dev_attr(devattr)->index))
0336             return 0;
0337     }
0338 
0339     return a->mode;
0340 }
0341 
0342 static struct attribute *max6650_attrs[] = {
0343     &sensor_dev_attr_gpio1_alarm.dev_attr.attr,
0344     &sensor_dev_attr_gpio2_alarm.dev_attr.attr,
0345     NULL
0346 };
0347 
0348 static const struct attribute_group max6650_group = {
0349     .attrs = max6650_attrs,
0350     .is_visible = max6650_attrs_visible,
0351 };
0352 
0353 static const struct attribute_group *max6650_groups[] = {
0354     &max6650_group,
0355     NULL
0356 };
0357 
0358 static int max6650_init_client(struct max6650_data *data,
0359                    struct i2c_client *client)
0360 {
0361     struct device *dev = &client->dev;
0362     int reg;
0363     int err;
0364     u32 voltage;
0365     u32 prescale;
0366     u32 target_rpm;
0367 
0368     if (of_property_read_u32(dev->of_node, "maxim,fan-microvolt",
0369                  &voltage))
0370         voltage = fan_voltage;
0371     else
0372         voltage /= 1000000; /* Microvolts to volts */
0373     if (of_property_read_u32(dev->of_node, "maxim,fan-prescale",
0374                  &prescale))
0375         prescale = prescaler;
0376 
0377     reg = i2c_smbus_read_byte_data(client, MAX6650_REG_CONFIG);
0378     if (reg < 0) {
0379         dev_err(dev, "Error reading config register, aborting.\n");
0380         return reg;
0381     }
0382 
0383     switch (voltage) {
0384     case 0:
0385         break;
0386     case 5:
0387         reg &= ~MAX6650_CFG_V12;
0388         break;
0389     case 12:
0390         reg |= MAX6650_CFG_V12;
0391         break;
0392     default:
0393         dev_err(dev, "illegal value for fan_voltage (%d)\n", voltage);
0394     }
0395 
0396     switch (prescale) {
0397     case 0:
0398         break;
0399     case 1:
0400         reg &= ~MAX6650_CFG_PRESCALER_MASK;
0401         break;
0402     case 2:
0403         reg = (reg & ~MAX6650_CFG_PRESCALER_MASK)
0404              | MAX6650_CFG_PRESCALER_2;
0405         break;
0406     case  4:
0407         reg = (reg & ~MAX6650_CFG_PRESCALER_MASK)
0408              | MAX6650_CFG_PRESCALER_4;
0409         break;
0410     case  8:
0411         reg = (reg & ~MAX6650_CFG_PRESCALER_MASK)
0412              | MAX6650_CFG_PRESCALER_8;
0413         break;
0414     case 16:
0415         reg = (reg & ~MAX6650_CFG_PRESCALER_MASK)
0416              | MAX6650_CFG_PRESCALER_16;
0417         break;
0418     default:
0419         dev_err(dev, "illegal value for prescaler (%d)\n", prescale);
0420     }
0421 
0422     dev_info(dev, "Fan voltage: %dV, prescaler: %d.\n",
0423          (reg & MAX6650_CFG_V12) ? 12 : 5,
0424          1 << (reg & MAX6650_CFG_PRESCALER_MASK));
0425 
0426     err = i2c_smbus_write_byte_data(client, MAX6650_REG_CONFIG, reg);
0427     if (err) {
0428         dev_err(dev, "Config write error, aborting.\n");
0429         return err;
0430     }
0431     data->config = reg;
0432 
0433     reg = i2c_smbus_read_byte_data(client, MAX6650_REG_SPEED);
0434     if (reg < 0) {
0435         dev_err(dev, "Failed to read speed register, aborting.\n");
0436         return reg;
0437     }
0438     data->speed = reg;
0439 
0440     reg = i2c_smbus_read_byte_data(client, MAX6650_REG_DAC);
0441     if (reg < 0) {
0442         dev_err(dev, "Failed to read DAC register, aborting.\n");
0443         return reg;
0444     }
0445     data->dac = reg;
0446 
0447     reg = i2c_smbus_read_byte_data(client, MAX6650_REG_COUNT);
0448     if (reg < 0) {
0449         dev_err(dev, "Failed to read count register, aborting.\n");
0450         return reg;
0451     }
0452     data->count = reg;
0453 
0454     reg = i2c_smbus_read_byte_data(client, MAX6650_REG_ALARM_EN);
0455     if (reg < 0) {
0456         dev_err(dev, "Failed to read alarm configuration, aborting.\n");
0457         return reg;
0458     }
0459     data->alarm_en = reg;
0460 
0461     if (!of_property_read_u32(client->dev.of_node, "maxim,fan-target-rpm",
0462                   &target_rpm)) {
0463         max6650_set_target(data, target_rpm);
0464         max6650_set_operating_mode(data, MAX6650_CFG_MODE_CLOSED_LOOP);
0465     }
0466 
0467     return 0;
0468 }
0469 
0470 static int max6650_get_max_state(struct thermal_cooling_device *cdev,
0471                  unsigned long *state)
0472 {
0473     *state = 255;
0474 
0475     return 0;
0476 }
0477 
0478 static int max6650_get_cur_state(struct thermal_cooling_device *cdev,
0479                  unsigned long *state)
0480 {
0481     struct max6650_data *data = cdev->devdata;
0482 
0483     *state = data->cooling_dev_state;
0484 
0485     return 0;
0486 }
0487 
0488 static int max6650_set_cur_state(struct thermal_cooling_device *cdev,
0489                  unsigned long state)
0490 {
0491     struct max6650_data *data = cdev->devdata;
0492     struct i2c_client *client = data->client;
0493     int err;
0494 
0495     state = clamp_val(state, 0, 255);
0496 
0497     mutex_lock(&data->update_lock);
0498 
0499     data->dac = pwm_to_dac(state, data->config & MAX6650_CFG_V12);
0500     err = i2c_smbus_write_byte_data(client, MAX6650_REG_DAC, data->dac);
0501     if (!err) {
0502         max6650_set_operating_mode(data, state ?
0503                        MAX6650_CFG_MODE_OPEN_LOOP :
0504                        MAX6650_CFG_MODE_OFF);
0505         data->cooling_dev_state = state;
0506     }
0507 
0508     mutex_unlock(&data->update_lock);
0509 
0510     return err;
0511 }
0512 
0513 static const struct thermal_cooling_device_ops max6650_cooling_ops = {
0514     .get_max_state = max6650_get_max_state,
0515     .get_cur_state = max6650_get_cur_state,
0516     .set_cur_state = max6650_set_cur_state,
0517 };
0518 
0519 static int max6650_read(struct device *dev, enum hwmon_sensor_types type,
0520             u32 attr, int channel, long *val)
0521 {
0522     struct max6650_data *data = max6650_update_device(dev);
0523     int mode;
0524 
0525     if (IS_ERR(data))
0526         return PTR_ERR(data);
0527 
0528     switch (type) {
0529     case hwmon_pwm:
0530         switch (attr) {
0531         case hwmon_pwm_input:
0532             *val = dac_to_pwm(data->dac,
0533                       data->config & MAX6650_CFG_V12);
0534             break;
0535         case hwmon_pwm_enable:
0536             /*
0537              * Possible values:
0538              * 0 = Fan always on
0539              * 1 = Open loop, Voltage is set according to speed,
0540              *     not regulated.
0541              * 2 = Closed loop, RPM for all fans regulated by fan1
0542              *     tachometer
0543              * 3 = Fan off
0544              */
0545             mode = (data->config & MAX6650_CFG_MODE_MASK) >> 4;
0546             *val = (4 - mode) & 3; /* {0 1 2 3} -> {0 3 2 1} */
0547             break;
0548         default:
0549             return -EOPNOTSUPP;
0550         }
0551         break;
0552     case hwmon_fan:
0553         switch (attr) {
0554         case hwmon_fan_input:
0555             /*
0556              * Calculation details:
0557              *
0558              * Each tachometer counts over an interval given by the
0559              * "count" register (0.25, 0.5, 1 or 2 seconds).
0560              * The driver assumes that the fans produce two pulses
0561              * per revolution (this seems to be the most common).
0562              */
0563             *val = DIV_ROUND_CLOSEST(data->tach[channel] * 120,
0564                          DIV_FROM_REG(data->count));
0565             break;
0566         case hwmon_fan_div:
0567             *val = DIV_FROM_REG(data->count);
0568             break;
0569         case hwmon_fan_target:
0570             /*
0571              * Use the datasheet equation:
0572              *    FanSpeed = KSCALE x fCLK / [256 x (KTACH + 1)]
0573              * then multiply by 60 to give rpm.
0574              */
0575             *val = 60 * DIV_FROM_REG(data->config) * clock /
0576                 (256 * (data->speed + 1));
0577             break;
0578         case hwmon_fan_min_alarm:
0579             *val = !!(data->alarm & MAX6650_ALRM_MIN);
0580             data->alarm &= ~MAX6650_ALRM_MIN;
0581             data->valid = false;
0582             break;
0583         case hwmon_fan_max_alarm:
0584             *val = !!(data->alarm & MAX6650_ALRM_MAX);
0585             data->alarm &= ~MAX6650_ALRM_MAX;
0586             data->valid = false;
0587             break;
0588         case hwmon_fan_fault:
0589             *val = !!(data->alarm & MAX6650_ALRM_TACH);
0590             data->alarm &= ~MAX6650_ALRM_TACH;
0591             data->valid = false;
0592             break;
0593         default:
0594             return -EOPNOTSUPP;
0595         }
0596         break;
0597     default:
0598         return -EOPNOTSUPP;
0599     }
0600     return 0;
0601 }
0602 
0603 static const u8 max6650_pwm_modes[] = {
0604     MAX6650_CFG_MODE_ON,
0605     MAX6650_CFG_MODE_OPEN_LOOP,
0606     MAX6650_CFG_MODE_CLOSED_LOOP,
0607     MAX6650_CFG_MODE_OFF,
0608 };
0609 
0610 static int max6650_write(struct device *dev, enum hwmon_sensor_types type,
0611              u32 attr, int channel, long val)
0612 {
0613     struct max6650_data *data = dev_get_drvdata(dev);
0614     int ret = 0;
0615     u8 reg;
0616 
0617     mutex_lock(&data->update_lock);
0618 
0619     switch (type) {
0620     case hwmon_pwm:
0621         switch (attr) {
0622         case hwmon_pwm_input:
0623             reg = pwm_to_dac(clamp_val(val, 0, 255),
0624                      data->config & MAX6650_CFG_V12);
0625             ret = i2c_smbus_write_byte_data(data->client,
0626                             MAX6650_REG_DAC, reg);
0627             if (ret)
0628                 break;
0629             data->dac = reg;
0630             break;
0631         case hwmon_pwm_enable:
0632             if (val < 0 || val >= ARRAY_SIZE(max6650_pwm_modes)) {
0633                 ret = -EINVAL;
0634                 break;
0635             }
0636             ret = max6650_set_operating_mode(data,
0637                         max6650_pwm_modes[val]);
0638             break;
0639         default:
0640             ret = -EOPNOTSUPP;
0641             break;
0642         }
0643         break;
0644     case hwmon_fan:
0645         switch (attr) {
0646         case hwmon_fan_div:
0647             switch (val) {
0648             case 1:
0649                 reg = 0;
0650                 break;
0651             case 2:
0652                 reg = 1;
0653                 break;
0654             case 4:
0655                 reg = 2;
0656                 break;
0657             case 8:
0658                 reg = 3;
0659                 break;
0660             default:
0661                 ret = -EINVAL;
0662                 goto error;
0663             }
0664             ret = i2c_smbus_write_byte_data(data->client,
0665                             MAX6650_REG_COUNT, reg);
0666             if (ret)
0667                 break;
0668             data->count = reg;
0669             break;
0670         case hwmon_fan_target:
0671             if (val < 0) {
0672                 ret = -EINVAL;
0673                 break;
0674             }
0675             ret = max6650_set_target(data, val);
0676             break;
0677         default:
0678             ret = -EOPNOTSUPP;
0679             break;
0680         }
0681         break;
0682     default:
0683         ret = -EOPNOTSUPP;
0684         break;
0685     }
0686 
0687 error:
0688     mutex_unlock(&data->update_lock);
0689     return ret;
0690 }
0691 
0692 static umode_t max6650_is_visible(const void *_data,
0693                   enum hwmon_sensor_types type, u32 attr,
0694                   int channel)
0695 {
0696     const struct max6650_data *data = _data;
0697 
0698     if (channel && (channel >= data->nr_fans || type != hwmon_fan))
0699         return 0;
0700 
0701     switch (type) {
0702     case hwmon_fan:
0703         switch (attr) {
0704         case hwmon_fan_input:
0705             return 0444;
0706         case hwmon_fan_target:
0707         case hwmon_fan_div:
0708             return 0644;
0709         case hwmon_fan_min_alarm:
0710             if (data->alarm_en & MAX6650_ALRM_MIN)
0711                 return 0444;
0712             break;
0713         case hwmon_fan_max_alarm:
0714             if (data->alarm_en & MAX6650_ALRM_MAX)
0715                 return 0444;
0716             break;
0717         case hwmon_fan_fault:
0718             if (data->alarm_en & MAX6650_ALRM_TACH)
0719                 return 0444;
0720             break;
0721         default:
0722             break;
0723         }
0724         break;
0725     case hwmon_pwm:
0726         switch (attr) {
0727         case hwmon_pwm_input:
0728         case hwmon_pwm_enable:
0729             return 0644;
0730         default:
0731             break;
0732         }
0733         break;
0734     default:
0735         break;
0736     }
0737     return 0;
0738 }
0739 
0740 static const struct hwmon_channel_info *max6650_info[] = {
0741     HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_DIV |
0742                HWMON_F_MIN_ALARM | HWMON_F_MAX_ALARM |
0743                HWMON_F_FAULT,
0744                HWMON_F_INPUT, HWMON_F_INPUT, HWMON_F_INPUT),
0745     HWMON_CHANNEL_INFO(pwm, HWMON_PWM_INPUT | HWMON_PWM_ENABLE),
0746     NULL
0747 };
0748 
0749 static const struct hwmon_ops max6650_hwmon_ops = {
0750     .read = max6650_read,
0751     .write = max6650_write,
0752     .is_visible = max6650_is_visible,
0753 };
0754 
0755 static const struct hwmon_chip_info max6650_chip_info = {
0756     .ops = &max6650_hwmon_ops,
0757     .info = max6650_info,
0758 };
0759 
0760 static const struct i2c_device_id max6650_id[];
0761 
0762 static int max6650_probe(struct i2c_client *client)
0763 {
0764     struct thermal_cooling_device *cooling_dev;
0765     struct device *dev = &client->dev;
0766     const struct of_device_id *of_id =
0767         of_match_device(of_match_ptr(max6650_dt_match), dev);
0768     struct max6650_data *data;
0769     struct device *hwmon_dev;
0770     int err;
0771 
0772     data = devm_kzalloc(dev, sizeof(struct max6650_data), GFP_KERNEL);
0773     if (!data)
0774         return -ENOMEM;
0775 
0776     data->client = client;
0777     i2c_set_clientdata(client, data);
0778     mutex_init(&data->update_lock);
0779     data->nr_fans = of_id ? (int)(uintptr_t)of_id->data :
0780                 i2c_match_id(max6650_id, client)->driver_data;
0781 
0782     /*
0783      * Initialize the max6650 chip
0784      */
0785     err = max6650_init_client(data, client);
0786     if (err)
0787         return err;
0788 
0789     hwmon_dev = devm_hwmon_device_register_with_info(dev,
0790                              client->name, data,
0791                              &max6650_chip_info,
0792                              max6650_groups);
0793     err = PTR_ERR_OR_ZERO(hwmon_dev);
0794     if (err)
0795         return err;
0796 
0797     if (IS_ENABLED(CONFIG_THERMAL)) {
0798         cooling_dev = devm_thermal_of_cooling_device_register(dev,
0799                         dev->of_node, client->name,
0800                         data, &max6650_cooling_ops);
0801         if (IS_ERR(cooling_dev)) {
0802             dev_warn(dev, "thermal cooling device register failed: %ld\n",
0803                  PTR_ERR(cooling_dev));
0804         }
0805     }
0806 
0807     return 0;
0808 }
0809 
0810 static const struct i2c_device_id max6650_id[] = {
0811     { "max6650", 1 },
0812     { "max6651", 4 },
0813     { }
0814 };
0815 MODULE_DEVICE_TABLE(i2c, max6650_id);
0816 
0817 static struct i2c_driver max6650_driver = {
0818     .driver = {
0819         .name   = "max6650",
0820         .of_match_table = of_match_ptr(max6650_dt_match),
0821     },
0822     .probe_new  = max6650_probe,
0823     .id_table   = max6650_id,
0824 };
0825 
0826 module_i2c_driver(max6650_driver);
0827 
0828 MODULE_AUTHOR("Hans J. Koch");
0829 MODULE_DESCRIPTION("MAX6650 sensor driver");
0830 MODULE_LICENSE("GPL");