Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Hardware monitoring driver for ucd9200 series Digital PWM System Controllers
0004  *
0005  * Copyright (C) 2011 Ericsson AB.
0006  */
0007 
0008 #include <linux/kernel.h>
0009 #include <linux/module.h>
0010 #include <linux/of_device.h>
0011 #include <linux/init.h>
0012 #include <linux/err.h>
0013 #include <linux/slab.h>
0014 #include <linux/i2c.h>
0015 #include <linux/pmbus.h>
0016 #include "pmbus.h"
0017 
0018 #define UCD9200_PHASE_INFO  0xd2
0019 #define UCD9200_DEVICE_ID   0xfd
0020 
0021 enum chips { ucd9200, ucd9220, ucd9222, ucd9224, ucd9240, ucd9244, ucd9246,
0022          ucd9248 };
0023 
0024 static const struct i2c_device_id ucd9200_id[] = {
0025     {"ucd9200", ucd9200},
0026     {"ucd9220", ucd9220},
0027     {"ucd9222", ucd9222},
0028     {"ucd9224", ucd9224},
0029     {"ucd9240", ucd9240},
0030     {"ucd9244", ucd9244},
0031     {"ucd9246", ucd9246},
0032     {"ucd9248", ucd9248},
0033     {}
0034 };
0035 MODULE_DEVICE_TABLE(i2c, ucd9200_id);
0036 
0037 static const struct of_device_id __maybe_unused ucd9200_of_match[] = {
0038     {
0039         .compatible = "ti,cd9200",
0040         .data = (void *)ucd9200
0041     },
0042     {
0043         .compatible = "ti,cd9220",
0044         .data = (void *)ucd9220
0045     },
0046     {
0047         .compatible = "ti,cd9222",
0048         .data = (void *)ucd9222
0049     },
0050     {
0051         .compatible = "ti,cd9224",
0052         .data = (void *)ucd9224
0053     },
0054     {
0055         .compatible = "ti,cd9240",
0056         .data = (void *)ucd9240
0057     },
0058     {
0059         .compatible = "ti,cd9244",
0060         .data = (void *)ucd9244
0061     },
0062     {
0063         .compatible = "ti,cd9246",
0064         .data = (void *)ucd9246
0065     },
0066     {
0067         .compatible = "ti,cd9248",
0068         .data = (void *)ucd9248
0069     },
0070     { },
0071 };
0072 MODULE_DEVICE_TABLE(of, ucd9200_of_match);
0073 
0074 static int ucd9200_probe(struct i2c_client *client)
0075 {
0076     u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
0077     struct pmbus_driver_info *info;
0078     const struct i2c_device_id *mid;
0079     enum chips chip;
0080     int i, j, ret;
0081 
0082     if (!i2c_check_functionality(client->adapter,
0083                      I2C_FUNC_SMBUS_BYTE_DATA |
0084                      I2C_FUNC_SMBUS_BLOCK_DATA))
0085         return -ENODEV;
0086 
0087     ret = i2c_smbus_read_block_data(client, UCD9200_DEVICE_ID,
0088                     block_buffer);
0089     if (ret < 0) {
0090         dev_err(&client->dev, "Failed to read device ID\n");
0091         return ret;
0092     }
0093     block_buffer[ret] = '\0';
0094     dev_info(&client->dev, "Device ID %s\n", block_buffer);
0095 
0096     for (mid = ucd9200_id; mid->name[0]; mid++) {
0097         if (!strncasecmp(mid->name, block_buffer, strlen(mid->name)))
0098             break;
0099     }
0100     if (!mid->name[0]) {
0101         dev_err(&client->dev, "Unsupported device\n");
0102         return -ENODEV;
0103     }
0104 
0105     if (client->dev.of_node)
0106         chip = (enum chips)of_device_get_match_data(&client->dev);
0107     else
0108         chip = mid->driver_data;
0109 
0110     if (chip != ucd9200 && strcmp(client->name, mid->name) != 0)
0111         dev_notice(&client->dev,
0112                "Device mismatch: Configured %s, detected %s\n",
0113                client->name, mid->name);
0114 
0115     info = devm_kzalloc(&client->dev, sizeof(struct pmbus_driver_info),
0116                 GFP_KERNEL);
0117     if (!info)
0118         return -ENOMEM;
0119 
0120     ret = i2c_smbus_read_block_data(client, UCD9200_PHASE_INFO,
0121                     block_buffer);
0122     if (ret < 0) {
0123         dev_err(&client->dev, "Failed to read phase information\n");
0124         return ret;
0125     }
0126 
0127     /*
0128      * Calculate number of configured pages (rails) from PHASE_INFO
0129      * register.
0130      * Rails have to be sequential, so we can abort after finding
0131      * the first unconfigured rail.
0132      */
0133     info->pages = 0;
0134     for (i = 0; i < ret; i++) {
0135         if (!block_buffer[i])
0136             break;
0137         info->pages++;
0138     }
0139     if (!info->pages) {
0140         dev_err(&client->dev, "No rails configured\n");
0141         return -ENODEV;
0142     }
0143     dev_info(&client->dev, "%d rails configured\n", info->pages);
0144 
0145     /*
0146      * Set PHASE registers on all pages to 0xff to ensure that phase
0147      * specific commands will apply to all phases of a given page (rail).
0148      * This only affects the READ_IOUT and READ_TEMPERATURE2 registers.
0149      * READ_IOUT will return the sum of currents of all phases of a rail,
0150      * and READ_TEMPERATURE2 will return the maximum temperature detected
0151      * for the phases of the rail.
0152      */
0153     for (i = 0; i < info->pages; i++) {
0154         /*
0155          * Setting PAGE & PHASE fails once in a while for no obvious
0156          * reason, so we need to retry a couple of times.
0157          */
0158         for (j = 0; j < 3; j++) {
0159             ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, i);
0160             if (ret < 0)
0161                 continue;
0162             ret = i2c_smbus_write_byte_data(client, PMBUS_PHASE,
0163                             0xff);
0164             if (ret < 0)
0165                 continue;
0166             break;
0167         }
0168         if (ret < 0) {
0169             dev_err(&client->dev,
0170                 "Failed to initialize PHASE registers\n");
0171             return ret;
0172         }
0173     }
0174     if (info->pages > 1)
0175         i2c_smbus_write_byte_data(client, PMBUS_PAGE, 0);
0176 
0177     info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT |
0178             PMBUS_HAVE_IIN | PMBUS_HAVE_PIN |
0179             PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
0180             PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
0181             PMBUS_HAVE_POUT | PMBUS_HAVE_TEMP |
0182             PMBUS_HAVE_TEMP2 | PMBUS_HAVE_STATUS_TEMP;
0183 
0184     for (i = 1; i < info->pages; i++)
0185         info->func[i] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
0186             PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
0187             PMBUS_HAVE_POUT |
0188             PMBUS_HAVE_TEMP2 | PMBUS_HAVE_STATUS_TEMP;
0189 
0190     /* ucd9240 supports a single fan */
0191     if (mid->driver_data == ucd9240)
0192         info->func[0] |= PMBUS_HAVE_FAN12 | PMBUS_HAVE_STATUS_FAN12;
0193 
0194     return pmbus_do_probe(client, info);
0195 }
0196 
0197 /* This is the driver that will be inserted */
0198 static struct i2c_driver ucd9200_driver = {
0199     .driver = {
0200         .name = "ucd9200",
0201         .of_match_table = of_match_ptr(ucd9200_of_match),
0202     },
0203     .probe_new = ucd9200_probe,
0204     .id_table = ucd9200_id,
0205 };
0206 
0207 module_i2c_driver(ucd9200_driver);
0208 
0209 MODULE_AUTHOR("Guenter Roeck");
0210 MODULE_DESCRIPTION("PMBus driver for TI UCD922x, UCD924x");
0211 MODULE_LICENSE("GPL");
0212 MODULE_IMPORT_NS(PMBUS);