Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Driver for MAX20710, MAX20730, MAX20734, and MAX20743 Integrated,
0004  * Step-Down Switching Regulators
0005  *
0006  * Copyright 2019 Google LLC.
0007  * Copyright 2020 Maxim Integrated
0008  */
0009 
0010 #include <linux/bits.h>
0011 #include <linux/debugfs.h>
0012 #include <linux/err.h>
0013 #include <linux/i2c.h>
0014 #include <linux/init.h>
0015 #include <linux/kernel.h>
0016 #include <linux/module.h>
0017 #include <linux/mutex.h>
0018 #include <linux/of_device.h>
0019 #include <linux/pmbus.h>
0020 #include <linux/util_macros.h>
0021 #include "pmbus.h"
0022 
0023 enum chips {
0024     max20710,
0025     max20730,
0026     max20734,
0027     max20743
0028 };
0029 
0030 enum {
0031     MAX20730_DEBUGFS_VOUT_MIN = 0,
0032     MAX20730_DEBUGFS_FREQUENCY,
0033     MAX20730_DEBUGFS_PG_DELAY,
0034     MAX20730_DEBUGFS_INTERNAL_GAIN,
0035     MAX20730_DEBUGFS_BOOT_VOLTAGE,
0036     MAX20730_DEBUGFS_OUT_V_RAMP_RATE,
0037     MAX20730_DEBUGFS_OC_PROTECT_MODE,
0038     MAX20730_DEBUGFS_SS_TIMING,
0039     MAX20730_DEBUGFS_IMAX,
0040     MAX20730_DEBUGFS_OPERATION,
0041     MAX20730_DEBUGFS_ON_OFF_CONFIG,
0042     MAX20730_DEBUGFS_SMBALERT_MASK,
0043     MAX20730_DEBUGFS_VOUT_MODE,
0044     MAX20730_DEBUGFS_VOUT_COMMAND,
0045     MAX20730_DEBUGFS_VOUT_MAX,
0046     MAX20730_DEBUGFS_NUM_ENTRIES
0047 };
0048 
0049 struct max20730_data {
0050     enum chips id;
0051     struct pmbus_driver_info info;
0052     struct mutex lock;  /* Used to protect against parallel writes */
0053     u16 mfr_devset1;
0054     u16 mfr_devset2;
0055     u16 mfr_voutmin;
0056     u32 vout_voltage_divider[2];
0057 };
0058 
0059 #define to_max20730_data(x)  container_of(x, struct max20730_data, info)
0060 
0061 #define VOLT_FROM_REG(val)  DIV_ROUND_CLOSEST((val), 1 << 9)
0062 
0063 #define PMBUS_SMB_ALERT_MASK    0x1B
0064 
0065 #define MAX20730_MFR_VOUT_MIN   0xd1
0066 #define MAX20730_MFR_DEVSET1    0xd2
0067 #define MAX20730_MFR_DEVSET2    0xd3
0068 
0069 #define MAX20730_MFR_VOUT_MIN_MASK      GENMASK(9, 0)
0070 #define MAX20730_MFR_VOUT_MIN_BIT_POS       0
0071 
0072 #define MAX20730_MFR_DEVSET1_RGAIN_MASK     (BIT(13) | BIT(14))
0073 #define MAX20730_MFR_DEVSET1_OTP_MASK       (BIT(11) | BIT(12))
0074 #define MAX20730_MFR_DEVSET1_VBOOT_MASK     (BIT(8) | BIT(9))
0075 #define MAX20730_MFR_DEVSET1_OCP_MASK       (BIT(5) | BIT(6))
0076 #define MAX20730_MFR_DEVSET1_FSW_MASK       GENMASK(4, 2)
0077 #define MAX20730_MFR_DEVSET1_TSTAT_MASK     (BIT(0) | BIT(1))
0078 
0079 #define MAX20730_MFR_DEVSET1_RGAIN_BIT_POS  13
0080 #define MAX20730_MFR_DEVSET1_OTP_BIT_POS    11
0081 #define MAX20730_MFR_DEVSET1_VBOOT_BIT_POS  8
0082 #define MAX20730_MFR_DEVSET1_OCP_BIT_POS    5
0083 #define MAX20730_MFR_DEVSET1_FSW_BIT_POS    2
0084 #define MAX20730_MFR_DEVSET1_TSTAT_BIT_POS  0
0085 
0086 #define MAX20730_MFR_DEVSET2_IMAX_MASK      GENMASK(10, 8)
0087 #define MAX20730_MFR_DEVSET2_VRATE      (BIT(6) | BIT(7))
0088 #define MAX20730_MFR_DEVSET2_OCPM_MASK      BIT(5)
0089 #define MAX20730_MFR_DEVSET2_SS_MASK        (BIT(0) | BIT(1))
0090 
0091 #define MAX20730_MFR_DEVSET2_IMAX_BIT_POS   8
0092 #define MAX20730_MFR_DEVSET2_VRATE_BIT_POS  6
0093 #define MAX20730_MFR_DEVSET2_OCPM_BIT_POS   5
0094 #define MAX20730_MFR_DEVSET2_SS_BIT_POS     0
0095 
0096 #define DEBUG_FS_DATA_MAX           16
0097 
0098 struct max20730_debugfs_data {
0099     struct i2c_client *client;
0100     int debugfs_entries[MAX20730_DEBUGFS_NUM_ENTRIES];
0101 };
0102 
0103 #define to_psu(x, y) container_of((x), \
0104             struct max20730_debugfs_data, debugfs_entries[(y)])
0105 
0106 #ifdef CONFIG_DEBUG_FS
0107 static ssize_t max20730_debugfs_read(struct file *file, char __user *buf,
0108                      size_t count, loff_t *ppos)
0109 {
0110     int ret, len;
0111     int *idxp = file->private_data;
0112     int idx = *idxp;
0113     struct max20730_debugfs_data *psu = to_psu(idxp, idx);
0114     const struct pmbus_driver_info *info;
0115     const struct max20730_data *data;
0116     char tbuf[DEBUG_FS_DATA_MAX] = { 0 };
0117     u16 val;
0118 
0119     info = pmbus_get_driver_info(psu->client);
0120     data = to_max20730_data(info);
0121 
0122     switch (idx) {
0123     case MAX20730_DEBUGFS_VOUT_MIN:
0124         ret = VOLT_FROM_REG(data->mfr_voutmin * 10000);
0125         len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d.%d\n",
0126                 ret / 10000, ret % 10000);
0127         break;
0128     case MAX20730_DEBUGFS_FREQUENCY:
0129         val = (data->mfr_devset1 & MAX20730_MFR_DEVSET1_FSW_MASK)
0130             >> MAX20730_MFR_DEVSET1_FSW_BIT_POS;
0131 
0132         if (val == 0)
0133             ret = 400;
0134         else if (val == 1)
0135             ret = 500;
0136         else if (val == 2 || val == 3)
0137             ret = 600;
0138         else if (val == 4)
0139             ret = 700;
0140         else if (val == 5)
0141             ret = 800;
0142         else
0143             ret = 900;
0144         len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
0145         break;
0146     case MAX20730_DEBUGFS_PG_DELAY:
0147         val = (data->mfr_devset1 & MAX20730_MFR_DEVSET1_TSTAT_MASK)
0148             >> MAX20730_MFR_DEVSET1_TSTAT_BIT_POS;
0149 
0150         if (val == 0)
0151             len = strlcpy(tbuf, "2000\n", DEBUG_FS_DATA_MAX);
0152         else if (val == 1)
0153             len = strlcpy(tbuf, "125\n", DEBUG_FS_DATA_MAX);
0154         else if (val == 2)
0155             len = strlcpy(tbuf, "62.5\n", DEBUG_FS_DATA_MAX);
0156         else
0157             len = strlcpy(tbuf, "32\n", DEBUG_FS_DATA_MAX);
0158         break;
0159     case MAX20730_DEBUGFS_INTERNAL_GAIN:
0160         val = (data->mfr_devset1 & MAX20730_MFR_DEVSET1_RGAIN_MASK)
0161             >> MAX20730_MFR_DEVSET1_RGAIN_BIT_POS;
0162 
0163         if (data->id == max20734) {
0164             /* AN6209 */
0165             if (val == 0)
0166                 len = strlcpy(tbuf, "0.8\n", DEBUG_FS_DATA_MAX);
0167             else if (val == 1)
0168                 len = strlcpy(tbuf, "3.2\n", DEBUG_FS_DATA_MAX);
0169             else if (val == 2)
0170                 len = strlcpy(tbuf, "1.6\n", DEBUG_FS_DATA_MAX);
0171             else
0172                 len = strlcpy(tbuf, "6.4\n", DEBUG_FS_DATA_MAX);
0173         } else if (data->id == max20730 || data->id == max20710) {
0174             /* AN6042 or AN6140 */
0175             if (val == 0)
0176                 len = strlcpy(tbuf, "0.9\n", DEBUG_FS_DATA_MAX);
0177             else if (val == 1)
0178                 len = strlcpy(tbuf, "3.6\n", DEBUG_FS_DATA_MAX);
0179             else if (val == 2)
0180                 len = strlcpy(tbuf, "1.8\n", DEBUG_FS_DATA_MAX);
0181             else
0182                 len = strlcpy(tbuf, "7.2\n", DEBUG_FS_DATA_MAX);
0183         } else if (data->id == max20743) {
0184             /* AN6042 */
0185             if (val == 0)
0186                 len = strlcpy(tbuf, "0.45\n", DEBUG_FS_DATA_MAX);
0187             else if (val == 1)
0188                 len = strlcpy(tbuf, "1.8\n", DEBUG_FS_DATA_MAX);
0189             else if (val == 2)
0190                 len = strlcpy(tbuf, "0.9\n", DEBUG_FS_DATA_MAX);
0191             else
0192                 len = strlcpy(tbuf, "3.6\n", DEBUG_FS_DATA_MAX);
0193         } else {
0194             len = strlcpy(tbuf, "Not supported\n", DEBUG_FS_DATA_MAX);
0195         }
0196         break;
0197     case MAX20730_DEBUGFS_BOOT_VOLTAGE:
0198         val = (data->mfr_devset1 & MAX20730_MFR_DEVSET1_VBOOT_MASK)
0199             >> MAX20730_MFR_DEVSET1_VBOOT_BIT_POS;
0200 
0201         if (val == 0)
0202             len = strlcpy(tbuf, "0.6484\n", DEBUG_FS_DATA_MAX);
0203         else if (val == 1)
0204             len = strlcpy(tbuf, "0.8984\n", DEBUG_FS_DATA_MAX);
0205         else if (val == 2)
0206             len = strlcpy(tbuf, "1.0\n", DEBUG_FS_DATA_MAX);
0207         else
0208             len = strlcpy(tbuf, "Invalid\n", DEBUG_FS_DATA_MAX);
0209         break;
0210     case MAX20730_DEBUGFS_OUT_V_RAMP_RATE:
0211         val = (data->mfr_devset2 & MAX20730_MFR_DEVSET2_VRATE)
0212             >> MAX20730_MFR_DEVSET2_VRATE_BIT_POS;
0213 
0214         if (val == 0)
0215             len = strlcpy(tbuf, "4\n", DEBUG_FS_DATA_MAX);
0216         else if (val == 1)
0217             len = strlcpy(tbuf, "2\n", DEBUG_FS_DATA_MAX);
0218         else if (val == 2)
0219             len = strlcpy(tbuf, "1\n", DEBUG_FS_DATA_MAX);
0220         else
0221             len = strlcpy(tbuf, "Invalid\n", DEBUG_FS_DATA_MAX);
0222         break;
0223     case MAX20730_DEBUGFS_OC_PROTECT_MODE:
0224         ret = (data->mfr_devset2 & MAX20730_MFR_DEVSET2_OCPM_MASK)
0225             >> MAX20730_MFR_DEVSET2_OCPM_BIT_POS;
0226         len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
0227         break;
0228     case MAX20730_DEBUGFS_SS_TIMING:
0229         val = (data->mfr_devset2 & MAX20730_MFR_DEVSET2_SS_MASK)
0230             >> MAX20730_MFR_DEVSET2_SS_BIT_POS;
0231 
0232         if (val == 0)
0233             len = strlcpy(tbuf, "0.75\n", DEBUG_FS_DATA_MAX);
0234         else if (val == 1)
0235             len = strlcpy(tbuf, "1.5\n", DEBUG_FS_DATA_MAX);
0236         else if (val == 2)
0237             len = strlcpy(tbuf, "3\n", DEBUG_FS_DATA_MAX);
0238         else
0239             len = strlcpy(tbuf, "6\n", DEBUG_FS_DATA_MAX);
0240         break;
0241     case MAX20730_DEBUGFS_IMAX:
0242         ret = (data->mfr_devset2 & MAX20730_MFR_DEVSET2_IMAX_MASK)
0243             >> MAX20730_MFR_DEVSET2_IMAX_BIT_POS;
0244         len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
0245         break;
0246     case MAX20730_DEBUGFS_OPERATION:
0247         ret = i2c_smbus_read_byte_data(psu->client, PMBUS_OPERATION);
0248         if (ret < 0)
0249             return ret;
0250         len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
0251         break;
0252     case MAX20730_DEBUGFS_ON_OFF_CONFIG:
0253         ret = i2c_smbus_read_byte_data(psu->client, PMBUS_ON_OFF_CONFIG);
0254         if (ret < 0)
0255             return ret;
0256         len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
0257         break;
0258     case MAX20730_DEBUGFS_SMBALERT_MASK:
0259         ret = i2c_smbus_read_word_data(psu->client,
0260                            PMBUS_SMB_ALERT_MASK);
0261         if (ret < 0)
0262             return ret;
0263         len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
0264         break;
0265     case MAX20730_DEBUGFS_VOUT_MODE:
0266         ret = i2c_smbus_read_byte_data(psu->client, PMBUS_VOUT_MODE);
0267         if (ret < 0)
0268             return ret;
0269         len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
0270         break;
0271     case MAX20730_DEBUGFS_VOUT_COMMAND:
0272         ret = i2c_smbus_read_word_data(psu->client, PMBUS_VOUT_COMMAND);
0273         if (ret < 0)
0274             return ret;
0275 
0276         ret = VOLT_FROM_REG(ret * 10000);
0277         len = scnprintf(tbuf, DEBUG_FS_DATA_MAX,
0278                 "%d.%d\n", ret / 10000, ret % 10000);
0279         break;
0280     case MAX20730_DEBUGFS_VOUT_MAX:
0281         ret = i2c_smbus_read_word_data(psu->client, PMBUS_VOUT_MAX);
0282         if (ret < 0)
0283             return ret;
0284 
0285         ret = VOLT_FROM_REG(ret * 10000);
0286         len = scnprintf(tbuf, DEBUG_FS_DATA_MAX,
0287                 "%d.%d\n", ret / 10000, ret % 10000);
0288         break;
0289     default:
0290         len = strlcpy(tbuf, "Invalid\n", DEBUG_FS_DATA_MAX);
0291     }
0292 
0293     return simple_read_from_buffer(buf, count, ppos, tbuf, len);
0294 }
0295 
0296 static const struct file_operations max20730_fops = {
0297     .llseek = noop_llseek,
0298     .read = max20730_debugfs_read,
0299     .write = NULL,
0300     .open = simple_open,
0301 };
0302 
0303 static int max20730_init_debugfs(struct i2c_client *client,
0304                  struct max20730_data *data)
0305 {
0306     int ret, i;
0307     struct dentry *debugfs;
0308     struct dentry *max20730_dir;
0309     struct max20730_debugfs_data *psu;
0310 
0311     ret = i2c_smbus_read_word_data(client, MAX20730_MFR_DEVSET2);
0312     if (ret < 0)
0313         return ret;
0314     data->mfr_devset2 = ret;
0315 
0316     ret = i2c_smbus_read_word_data(client, MAX20730_MFR_VOUT_MIN);
0317     if (ret < 0)
0318         return ret;
0319     data->mfr_voutmin = ret;
0320 
0321     psu = devm_kzalloc(&client->dev, sizeof(*psu), GFP_KERNEL);
0322     if (!psu)
0323         return -ENOMEM;
0324     psu->client = client;
0325 
0326     debugfs = pmbus_get_debugfs_dir(client);
0327     if (!debugfs)
0328         return -ENOENT;
0329 
0330     max20730_dir = debugfs_create_dir(client->name, debugfs);
0331 
0332     for (i = 0; i < MAX20730_DEBUGFS_NUM_ENTRIES; ++i)
0333         psu->debugfs_entries[i] = i;
0334 
0335     debugfs_create_file("vout_min", 0444, max20730_dir,
0336                 &psu->debugfs_entries[MAX20730_DEBUGFS_VOUT_MIN],
0337                 &max20730_fops);
0338     debugfs_create_file("frequency", 0444, max20730_dir,
0339                 &psu->debugfs_entries[MAX20730_DEBUGFS_FREQUENCY],
0340                 &max20730_fops);
0341     debugfs_create_file("power_good_delay", 0444, max20730_dir,
0342                 &psu->debugfs_entries[MAX20730_DEBUGFS_PG_DELAY],
0343                 &max20730_fops);
0344     debugfs_create_file("internal_gain", 0444, max20730_dir,
0345                 &psu->debugfs_entries[MAX20730_DEBUGFS_INTERNAL_GAIN],
0346                 &max20730_fops);
0347     debugfs_create_file("boot_voltage", 0444, max20730_dir,
0348                 &psu->debugfs_entries[MAX20730_DEBUGFS_BOOT_VOLTAGE],
0349                 &max20730_fops);
0350     debugfs_create_file("out_voltage_ramp_rate", 0444, max20730_dir,
0351                 &psu->debugfs_entries[MAX20730_DEBUGFS_OUT_V_RAMP_RATE],
0352                 &max20730_fops);
0353     debugfs_create_file("oc_protection_mode", 0444, max20730_dir,
0354                 &psu->debugfs_entries[MAX20730_DEBUGFS_OC_PROTECT_MODE],
0355                 &max20730_fops);
0356     debugfs_create_file("soft_start_timing", 0444, max20730_dir,
0357                 &psu->debugfs_entries[MAX20730_DEBUGFS_SS_TIMING],
0358                 &max20730_fops);
0359     debugfs_create_file("imax", 0444, max20730_dir,
0360                 &psu->debugfs_entries[MAX20730_DEBUGFS_IMAX],
0361                 &max20730_fops);
0362     debugfs_create_file("operation", 0444, max20730_dir,
0363                 &psu->debugfs_entries[MAX20730_DEBUGFS_OPERATION],
0364                 &max20730_fops);
0365     debugfs_create_file("on_off_config", 0444, max20730_dir,
0366                 &psu->debugfs_entries[MAX20730_DEBUGFS_ON_OFF_CONFIG],
0367                 &max20730_fops);
0368     debugfs_create_file("smbalert_mask", 0444, max20730_dir,
0369                 &psu->debugfs_entries[MAX20730_DEBUGFS_SMBALERT_MASK],
0370                 &max20730_fops);
0371     debugfs_create_file("vout_mode", 0444, max20730_dir,
0372                 &psu->debugfs_entries[MAX20730_DEBUGFS_VOUT_MODE],
0373                 &max20730_fops);
0374     debugfs_create_file("vout_command", 0444, max20730_dir,
0375                 &psu->debugfs_entries[MAX20730_DEBUGFS_VOUT_COMMAND],
0376                 &max20730_fops);
0377     debugfs_create_file("vout_max", 0444, max20730_dir,
0378                 &psu->debugfs_entries[MAX20730_DEBUGFS_VOUT_MAX],
0379                 &max20730_fops);
0380 
0381     return 0;
0382 }
0383 #else
0384 static int max20730_init_debugfs(struct i2c_client *client,
0385                  struct max20730_data *data)
0386 {
0387     return 0;
0388 }
0389 #endif /* CONFIG_DEBUG_FS */
0390 
0391 static const struct i2c_device_id max20730_id[];
0392 
0393 /*
0394  * Convert discreet value to direct data format. Strictly speaking, all passed
0395  * values are constants, so we could do that calculation manually. On the
0396  * downside, that would make the driver more difficult to maintain, so lets
0397  * use this approach.
0398  */
0399 static u16 val_to_direct(int v, enum pmbus_sensor_classes class,
0400              const struct pmbus_driver_info *info)
0401 {
0402     int R = info->R[class] - 3; /* take milli-units into account */
0403     int b = info->b[class] * 1000;
0404     long d;
0405 
0406     d = v * info->m[class] + b;
0407     /*
0408      * R < 0 is true for all callers, so we don't need to bother
0409      * about the R > 0 case.
0410      */
0411     while (R < 0) {
0412         d = DIV_ROUND_CLOSEST(d, 10);
0413         R++;
0414     }
0415     return (u16)d;
0416 }
0417 
0418 static long direct_to_val(u16 w, enum pmbus_sensor_classes class,
0419               const struct pmbus_driver_info *info)
0420 {
0421     int R = info->R[class] - 3;
0422     int b = info->b[class] * 1000;
0423     int m = info->m[class];
0424     long d = (s16)w;
0425 
0426     if (m == 0)
0427         return 0;
0428 
0429     while (R < 0) {
0430         d *= 10;
0431         R++;
0432     }
0433     d = (d - b) / m;
0434     return d;
0435 }
0436 
0437 static u32 max_current[][5] = {
0438     [max20710] = { 6200, 8000, 9700, 11600 },
0439     [max20730] = { 13000, 16600, 20100, 23600 },
0440     [max20734] = { 21000, 27000, 32000, 38000 },
0441     [max20743] = { 18900, 24100, 29200, 34100 },
0442 };
0443 
0444 static int max20730_read_word_data(struct i2c_client *client, int page,
0445                    int phase, int reg)
0446 {
0447     const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
0448     const struct max20730_data *data = to_max20730_data(info);
0449     int ret = 0;
0450     u32 max_c;
0451 
0452     switch (reg) {
0453     case PMBUS_OT_FAULT_LIMIT:
0454         switch ((data->mfr_devset1 >> 11) & 0x3) {
0455         case 0x0:
0456             ret = val_to_direct(150000, PSC_TEMPERATURE, info);
0457             break;
0458         case 0x1:
0459             ret = val_to_direct(130000, PSC_TEMPERATURE, info);
0460             break;
0461         default:
0462             ret = -ENODATA;
0463             break;
0464         }
0465         break;
0466     case PMBUS_IOUT_OC_FAULT_LIMIT:
0467         max_c = max_current[data->id][(data->mfr_devset1 >> 5) & 0x3];
0468         ret = val_to_direct(max_c, PSC_CURRENT_OUT, info);
0469         break;
0470     case PMBUS_READ_VOUT:
0471         ret = pmbus_read_word_data(client, page, phase, reg);
0472         if (ret > 0 && data->vout_voltage_divider[0] && data->vout_voltage_divider[1]) {
0473             u64 temp = DIV_ROUND_CLOSEST_ULL((u64)ret * data->vout_voltage_divider[1],
0474                              data->vout_voltage_divider[0]);
0475             ret = clamp_val(temp, 0, 0xffff);
0476         }
0477         break;
0478     default:
0479         ret = -ENODATA;
0480         break;
0481     }
0482     return ret;
0483 }
0484 
0485 static int max20730_write_word_data(struct i2c_client *client, int page,
0486                     int reg, u16 word)
0487 {
0488     struct pmbus_driver_info *info;
0489     struct max20730_data *data;
0490     u16 devset1;
0491     int ret = 0;
0492     int idx;
0493 
0494     info = (struct pmbus_driver_info *)pmbus_get_driver_info(client);
0495     data = to_max20730_data(info);
0496 
0497     mutex_lock(&data->lock);
0498     devset1 = data->mfr_devset1;
0499 
0500     switch (reg) {
0501     case PMBUS_OT_FAULT_LIMIT:
0502         devset1 &= ~(BIT(11) | BIT(12));
0503         if (direct_to_val(word, PSC_TEMPERATURE, info) < 140000)
0504             devset1 |= BIT(11);
0505         break;
0506     case PMBUS_IOUT_OC_FAULT_LIMIT:
0507         devset1 &= ~(BIT(5) | BIT(6));
0508 
0509         idx = find_closest(direct_to_val(word, PSC_CURRENT_OUT, info),
0510                    max_current[data->id], 4);
0511         devset1 |= (idx << 5);
0512         break;
0513     default:
0514         ret = -ENODATA;
0515         break;
0516     }
0517 
0518     if (!ret && devset1 != data->mfr_devset1) {
0519         ret = i2c_smbus_write_word_data(client, MAX20730_MFR_DEVSET1,
0520                         devset1);
0521         if (!ret) {
0522             data->mfr_devset1 = devset1;
0523             pmbus_clear_cache(client);
0524         }
0525     }
0526     mutex_unlock(&data->lock);
0527     return ret;
0528 }
0529 
0530 static const struct pmbus_driver_info max20730_info[] = {
0531     [max20710] = {
0532         .pages = 1,
0533         .read_word_data = max20730_read_word_data,
0534         .write_word_data = max20730_write_word_data,
0535 
0536         /* Source : Maxim AN6140 and AN6042 */
0537         .format[PSC_TEMPERATURE] = direct,
0538         .m[PSC_TEMPERATURE] = 21,
0539         .b[PSC_TEMPERATURE] = 5887,
0540         .R[PSC_TEMPERATURE] = -1,
0541 
0542         .format[PSC_VOLTAGE_IN] = direct,
0543         .m[PSC_VOLTAGE_IN] = 3609,
0544         .b[PSC_VOLTAGE_IN] = 0,
0545         .R[PSC_VOLTAGE_IN] = -2,
0546 
0547         .format[PSC_CURRENT_OUT] = direct,
0548         .m[PSC_CURRENT_OUT] = 153,
0549         .b[PSC_CURRENT_OUT] = 4976,
0550         .R[PSC_CURRENT_OUT] = -1,
0551 
0552         .format[PSC_VOLTAGE_OUT] = linear,
0553 
0554         .func[0] = PMBUS_HAVE_VIN |
0555             PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
0556             PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
0557             PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP |
0558             PMBUS_HAVE_STATUS_INPUT,
0559     },
0560     [max20730] = {
0561         .pages = 1,
0562         .read_word_data = max20730_read_word_data,
0563         .write_word_data = max20730_write_word_data,
0564 
0565         /* Source : Maxim AN6042 */
0566         .format[PSC_TEMPERATURE] = direct,
0567         .m[PSC_TEMPERATURE] = 21,
0568         .b[PSC_TEMPERATURE] = 5887,
0569         .R[PSC_TEMPERATURE] = -1,
0570 
0571         .format[PSC_VOLTAGE_IN] = direct,
0572         .m[PSC_VOLTAGE_IN] = 3609,
0573         .b[PSC_VOLTAGE_IN] = 0,
0574         .R[PSC_VOLTAGE_IN] = -2,
0575 
0576         /*
0577          * Values in the datasheet are adjusted for temperature and
0578          * for the relationship between Vin and Vout.
0579          * Unfortunately, the data sheet suggests that Vout measurement
0580          * may be scaled with a resistor array. This is indeed the case
0581          * at least on the evaulation boards. As a result, any in-driver
0582          * adjustments would either be wrong or require elaborate means
0583          * to configure the scaling. Instead of doing that, just report
0584          * raw values and let userspace handle adjustments.
0585          */
0586         .format[PSC_CURRENT_OUT] = direct,
0587         .m[PSC_CURRENT_OUT] = 153,
0588         .b[PSC_CURRENT_OUT] = 4976,
0589         .R[PSC_CURRENT_OUT] = -1,
0590 
0591         .format[PSC_VOLTAGE_OUT] = linear,
0592 
0593         .func[0] = PMBUS_HAVE_VIN |
0594             PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
0595             PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
0596             PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP |
0597             PMBUS_HAVE_STATUS_INPUT,
0598     },
0599     [max20734] = {
0600         .pages = 1,
0601         .read_word_data = max20730_read_word_data,
0602         .write_word_data = max20730_write_word_data,
0603 
0604         /* Source : Maxim AN6209 */
0605         .format[PSC_TEMPERATURE] = direct,
0606         .m[PSC_TEMPERATURE] = 21,
0607         .b[PSC_TEMPERATURE] = 5887,
0608         .R[PSC_TEMPERATURE] = -1,
0609 
0610         .format[PSC_VOLTAGE_IN] = direct,
0611         .m[PSC_VOLTAGE_IN] = 3592,
0612         .b[PSC_VOLTAGE_IN] = 0,
0613         .R[PSC_VOLTAGE_IN] = -2,
0614 
0615         .format[PSC_CURRENT_OUT] = direct,
0616         .m[PSC_CURRENT_OUT] = 111,
0617         .b[PSC_CURRENT_OUT] = 3461,
0618         .R[PSC_CURRENT_OUT] = -1,
0619 
0620         .format[PSC_VOLTAGE_OUT] = linear,
0621 
0622         .func[0] = PMBUS_HAVE_VIN |
0623             PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
0624             PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
0625             PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP |
0626             PMBUS_HAVE_STATUS_INPUT,
0627     },
0628     [max20743] = {
0629         .pages = 1,
0630         .read_word_data = max20730_read_word_data,
0631         .write_word_data = max20730_write_word_data,
0632 
0633         /* Source : Maxim AN6042 */
0634         .format[PSC_TEMPERATURE] = direct,
0635         .m[PSC_TEMPERATURE] = 21,
0636         .b[PSC_TEMPERATURE] = 5887,
0637         .R[PSC_TEMPERATURE] = -1,
0638 
0639         .format[PSC_VOLTAGE_IN] = direct,
0640         .m[PSC_VOLTAGE_IN] = 3597,
0641         .b[PSC_VOLTAGE_IN] = 0,
0642         .R[PSC_VOLTAGE_IN] = -2,
0643 
0644         .format[PSC_CURRENT_OUT] = direct,
0645         .m[PSC_CURRENT_OUT] = 95,
0646         .b[PSC_CURRENT_OUT] = 5014,
0647         .R[PSC_CURRENT_OUT] = -1,
0648 
0649         .format[PSC_VOLTAGE_OUT] = linear,
0650 
0651         .func[0] = PMBUS_HAVE_VIN |
0652             PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
0653             PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
0654             PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP |
0655             PMBUS_HAVE_STATUS_INPUT,
0656     },
0657 };
0658 
0659 static int max20730_probe(struct i2c_client *client)
0660 {
0661     struct device *dev = &client->dev;
0662     u8 buf[I2C_SMBUS_BLOCK_MAX + 1];
0663     struct max20730_data *data;
0664     enum chips chip_id;
0665     int ret;
0666 
0667     if (!i2c_check_functionality(client->adapter,
0668                      I2C_FUNC_SMBUS_READ_BYTE_DATA |
0669                      I2C_FUNC_SMBUS_READ_WORD_DATA |
0670                      I2C_FUNC_SMBUS_BLOCK_DATA))
0671         return -ENODEV;
0672 
0673     ret = i2c_smbus_read_block_data(client, PMBUS_MFR_ID, buf);
0674     if (ret < 0) {
0675         dev_err(&client->dev, "Failed to read Manufacturer ID\n");
0676         return ret;
0677     }
0678     if (ret != 5 || strncmp(buf, "MAXIM", 5)) {
0679         buf[ret] = '\0';
0680         dev_err(dev, "Unsupported Manufacturer ID '%s'\n", buf);
0681         return -ENODEV;
0682     }
0683 
0684     /*
0685      * The chips support reading PMBUS_MFR_MODEL. On both MAX20730
0686      * and MAX20734, reading it returns M20743. Presumably that is
0687      * the reason why the command is not documented. Unfortunately,
0688      * that means that there is no reliable means to detect the chip.
0689      * However, we can at least detect the chip series. Compare
0690      * the returned value against 'M20743' and bail out if there is
0691      * a mismatch. If that doesn't work for all chips, we may have
0692      * to remove this check.
0693      */
0694     ret = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, buf);
0695     if (ret < 0) {
0696         dev_err(dev, "Failed to read Manufacturer Model\n");
0697         return ret;
0698     }
0699     if (ret != 6 || strncmp(buf, "M20743", 6)) {
0700         buf[ret] = '\0';
0701         dev_err(dev, "Unsupported Manufacturer Model '%s'\n", buf);
0702         return -ENODEV;
0703     }
0704 
0705     ret = i2c_smbus_read_block_data(client, PMBUS_MFR_REVISION, buf);
0706     if (ret < 0) {
0707         dev_err(dev, "Failed to read Manufacturer Revision\n");
0708         return ret;
0709     }
0710     if (ret != 1 || buf[0] != 'F') {
0711         buf[ret] = '\0';
0712         dev_err(dev, "Unsupported Manufacturer Revision '%s'\n", buf);
0713         return -ENODEV;
0714     }
0715 
0716     if (client->dev.of_node)
0717         chip_id = (enum chips)of_device_get_match_data(dev);
0718     else
0719         chip_id = i2c_match_id(max20730_id, client)->driver_data;
0720 
0721     data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
0722     if (!data)
0723         return -ENOMEM;
0724     data->id = chip_id;
0725     mutex_init(&data->lock);
0726     memcpy(&data->info, &max20730_info[chip_id], sizeof(data->info));
0727     if (of_property_read_u32_array(client->dev.of_node, "vout-voltage-divider",
0728                        data->vout_voltage_divider,
0729                        ARRAY_SIZE(data->vout_voltage_divider)) != 0)
0730         memset(data->vout_voltage_divider, 0, sizeof(data->vout_voltage_divider));
0731     if (data->vout_voltage_divider[1] < data->vout_voltage_divider[0]) {
0732         dev_err(dev,
0733             "The total resistance of voltage divider is less than output resistance\n");
0734         return -EINVAL;
0735     }
0736 
0737     ret = i2c_smbus_read_word_data(client, MAX20730_MFR_DEVSET1);
0738     if (ret < 0)
0739         return ret;
0740     data->mfr_devset1 = ret;
0741 
0742     ret = pmbus_do_probe(client, &data->info);
0743     if (ret < 0)
0744         return ret;
0745 
0746     ret = max20730_init_debugfs(client, data);
0747     if (ret)
0748         dev_warn(dev, "Failed to register debugfs: %d\n",
0749              ret);
0750 
0751     return 0;
0752 }
0753 
0754 static const struct i2c_device_id max20730_id[] = {
0755     { "max20710", max20710 },
0756     { "max20730", max20730 },
0757     { "max20734", max20734 },
0758     { "max20743", max20743 },
0759     { },
0760 };
0761 
0762 MODULE_DEVICE_TABLE(i2c, max20730_id);
0763 
0764 static const struct of_device_id max20730_of_match[] = {
0765     { .compatible = "maxim,max20710", .data = (void *)max20710 },
0766     { .compatible = "maxim,max20730", .data = (void *)max20730 },
0767     { .compatible = "maxim,max20734", .data = (void *)max20734 },
0768     { .compatible = "maxim,max20743", .data = (void *)max20743 },
0769     { },
0770 };
0771 
0772 MODULE_DEVICE_TABLE(of, max20730_of_match);
0773 
0774 static struct i2c_driver max20730_driver = {
0775     .driver = {
0776         .name = "max20730",
0777         .of_match_table = max20730_of_match,
0778     },
0779     .probe_new = max20730_probe,
0780     .id_table = max20730_id,
0781 };
0782 
0783 module_i2c_driver(max20730_driver);
0784 
0785 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
0786 MODULE_DESCRIPTION("PMBus driver for Maxim MAX20710 / MAX20730 / MAX20734 / MAX20743");
0787 MODULE_LICENSE("GPL");
0788 MODULE_IMPORT_NS(PMBUS);