| Guenter Roeck | 84fc458 | 2011-03-06 15:54:27 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Hardware monitoring driver for ucd9200 series Digital PWM System Controllers | 
|  | 3 | * | 
|  | 4 | * Copyright (C) 2011 Ericsson AB. | 
|  | 5 | * | 
|  | 6 | * This program is free software; you can redistribute it and/or modify | 
|  | 7 | * it under the terms of the GNU General Public License as published by | 
|  | 8 | * the Free Software Foundation; either version 2 of the License, or | 
|  | 9 | * (at your option) any later version. | 
|  | 10 | * | 
|  | 11 | * This program is distributed in the hope that it will be useful, | 
|  | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
|  | 14 | * GNU General Public License for more details. | 
|  | 15 | * | 
|  | 16 | * You should have received a copy of the GNU General Public License | 
|  | 17 | * along with this program; if not, write to the Free Software | 
|  | 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 
|  | 19 | */ | 
|  | 20 |  | 
|  | 21 | #include <linux/kernel.h> | 
|  | 22 | #include <linux/module.h> | 
|  | 23 | #include <linux/init.h> | 
|  | 24 | #include <linux/err.h> | 
|  | 25 | #include <linux/slab.h> | 
|  | 26 | #include <linux/i2c.h> | 
|  | 27 | #include <linux/i2c/pmbus.h> | 
|  | 28 | #include "pmbus.h" | 
|  | 29 |  | 
|  | 30 | #define UCD9200_PHASE_INFO	0xd2 | 
|  | 31 | #define UCD9200_DEVICE_ID	0xfd | 
|  | 32 |  | 
|  | 33 | enum chips { ucd9200, ucd9220, ucd9222, ucd9224, ucd9240, ucd9244, ucd9246, | 
|  | 34 | ucd9248 }; | 
|  | 35 |  | 
|  | 36 | static const struct i2c_device_id ucd9200_id[] = { | 
|  | 37 | {"ucd9200", ucd9200}, | 
|  | 38 | {"ucd9220", ucd9220}, | 
|  | 39 | {"ucd9222", ucd9222}, | 
|  | 40 | {"ucd9224", ucd9224}, | 
|  | 41 | {"ucd9240", ucd9240}, | 
|  | 42 | {"ucd9244", ucd9244}, | 
|  | 43 | {"ucd9246", ucd9246}, | 
|  | 44 | {"ucd9248", ucd9248}, | 
|  | 45 | {} | 
|  | 46 | }; | 
|  | 47 | MODULE_DEVICE_TABLE(i2c, ucd9200_id); | 
|  | 48 |  | 
|  | 49 | static int ucd9200_probe(struct i2c_client *client, | 
|  | 50 | const struct i2c_device_id *id) | 
|  | 51 | { | 
|  | 52 | u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1]; | 
|  | 53 | struct pmbus_driver_info *info; | 
|  | 54 | const struct i2c_device_id *mid; | 
|  | 55 | int i, j, ret; | 
|  | 56 |  | 
|  | 57 | if (!i2c_check_functionality(client->adapter, | 
|  | 58 | I2C_FUNC_SMBUS_BYTE_DATA | | 
|  | 59 | I2C_FUNC_SMBUS_BLOCK_DATA)) | 
|  | 60 | return -ENODEV; | 
|  | 61 |  | 
|  | 62 | ret = i2c_smbus_read_block_data(client, UCD9200_DEVICE_ID, | 
|  | 63 | block_buffer); | 
|  | 64 | if (ret < 0) { | 
|  | 65 | dev_err(&client->dev, "Failed to read device ID\n"); | 
|  | 66 | return ret; | 
|  | 67 | } | 
|  | 68 | block_buffer[ret] = '\0'; | 
|  | 69 | dev_info(&client->dev, "Device ID %s\n", block_buffer); | 
|  | 70 |  | 
| Jean Delvare | f020b00 | 2011-08-31 11:53:41 -0400 | [diff] [blame] | 71 | for (mid = ucd9200_id; mid->name[0]; mid++) { | 
| Guenter Roeck | 84fc458 | 2011-03-06 15:54:27 -0800 | [diff] [blame] | 72 | if (!strncasecmp(mid->name, block_buffer, strlen(mid->name))) | 
|  | 73 | break; | 
|  | 74 | } | 
| Jean Delvare | f020b00 | 2011-08-31 11:53:41 -0400 | [diff] [blame] | 75 | if (!mid->name[0]) { | 
| Guenter Roeck | 84fc458 | 2011-03-06 15:54:27 -0800 | [diff] [blame] | 76 | dev_err(&client->dev, "Unsupported device\n"); | 
|  | 77 | return -ENODEV; | 
|  | 78 | } | 
|  | 79 | if (id->driver_data != ucd9200 && id->driver_data != mid->driver_data) | 
|  | 80 | dev_notice(&client->dev, | 
|  | 81 | "Device mismatch: Configured %s, detected %s\n", | 
|  | 82 | id->name, mid->name); | 
|  | 83 |  | 
|  | 84 | info = kzalloc(sizeof(struct pmbus_driver_info), GFP_KERNEL); | 
|  | 85 | if (!info) | 
|  | 86 | return -ENOMEM; | 
|  | 87 |  | 
|  | 88 | ret = i2c_smbus_read_block_data(client, UCD9200_PHASE_INFO, | 
|  | 89 | block_buffer); | 
|  | 90 | if (ret < 0) { | 
|  | 91 | dev_err(&client->dev, "Failed to read phase information\n"); | 
|  | 92 | goto out; | 
|  | 93 | } | 
|  | 94 |  | 
|  | 95 | /* | 
|  | 96 | * Calculate number of configured pages (rails) from PHASE_INFO | 
|  | 97 | * register. | 
|  | 98 | * Rails have to be sequential, so we can abort after finding | 
|  | 99 | * the first unconfigured rail. | 
|  | 100 | */ | 
|  | 101 | info->pages = 0; | 
|  | 102 | for (i = 0; i < ret; i++) { | 
|  | 103 | if (!block_buffer[i]) | 
|  | 104 | break; | 
|  | 105 | info->pages++; | 
|  | 106 | } | 
|  | 107 | if (!info->pages) { | 
|  | 108 | dev_err(&client->dev, "No rails configured\n"); | 
|  | 109 | ret = -ENODEV; | 
|  | 110 | goto out; | 
|  | 111 | } | 
|  | 112 | dev_info(&client->dev, "%d rails configured\n", info->pages); | 
|  | 113 |  | 
|  | 114 | /* | 
|  | 115 | * Set PHASE registers on all pages to 0xff to ensure that phase | 
|  | 116 | * specific commands will apply to all phases of a given page (rail). | 
|  | 117 | * This only affects the READ_IOUT and READ_TEMPERATURE2 registers. | 
|  | 118 | * READ_IOUT will return the sum of currents of all phases of a rail, | 
|  | 119 | * and READ_TEMPERATURE2 will return the maximum temperature detected | 
|  | 120 | * for the the phases of the rail. | 
|  | 121 | */ | 
|  | 122 | for (i = 0; i < info->pages; i++) { | 
|  | 123 | /* | 
|  | 124 | * Setting PAGE & PHASE fails once in a while for no obvious | 
|  | 125 | * reason, so we need to retry a couple of times. | 
|  | 126 | */ | 
|  | 127 | for (j = 0; j < 3; j++) { | 
|  | 128 | ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, i); | 
|  | 129 | if (ret < 0) | 
|  | 130 | continue; | 
|  | 131 | ret = i2c_smbus_write_byte_data(client, PMBUS_PHASE, | 
|  | 132 | 0xff); | 
|  | 133 | if (ret < 0) | 
|  | 134 | continue; | 
|  | 135 | break; | 
|  | 136 | } | 
|  | 137 | if (ret < 0) { | 
|  | 138 | dev_err(&client->dev, | 
|  | 139 | "Failed to initialize PHASE registers\n"); | 
|  | 140 | goto out; | 
|  | 141 | } | 
|  | 142 | } | 
|  | 143 | if (info->pages > 1) | 
|  | 144 | i2c_smbus_write_byte_data(client, PMBUS_PAGE, 0); | 
|  | 145 |  | 
|  | 146 | info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT | | 
|  | 147 | PMBUS_HAVE_IIN | PMBUS_HAVE_PIN | | 
|  | 148 | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT | | 
|  | 149 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT | | 
|  | 150 | PMBUS_HAVE_POUT | PMBUS_HAVE_TEMP | | 
|  | 151 | PMBUS_HAVE_TEMP2 | PMBUS_HAVE_STATUS_TEMP; | 
|  | 152 |  | 
|  | 153 | for (i = 1; i < info->pages; i++) | 
|  | 154 | info->func[i] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT | | 
|  | 155 | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT | | 
|  | 156 | PMBUS_HAVE_POUT | | 
|  | 157 | PMBUS_HAVE_TEMP2 | PMBUS_HAVE_STATUS_TEMP; | 
|  | 158 |  | 
|  | 159 | /* ucd9240 supports a single fan */ | 
|  | 160 | if (mid->driver_data == ucd9240) | 
|  | 161 | info->func[0] |= PMBUS_HAVE_FAN12 | PMBUS_HAVE_STATUS_FAN12; | 
|  | 162 |  | 
|  | 163 | ret = pmbus_do_probe(client, mid, info); | 
|  | 164 | if (ret < 0) | 
|  | 165 | goto out; | 
|  | 166 | return 0; | 
|  | 167 | out: | 
|  | 168 | kfree(info); | 
|  | 169 | return ret; | 
|  | 170 | } | 
|  | 171 |  | 
|  | 172 | static int ucd9200_remove(struct i2c_client *client) | 
|  | 173 | { | 
| Guenter Roeck | 84fc458 | 2011-03-06 15:54:27 -0800 | [diff] [blame] | 174 | const struct pmbus_driver_info *info; | 
|  | 175 |  | 
|  | 176 | info = pmbus_get_driver_info(client); | 
| Guenter Roeck | 866cf12 | 2011-08-26 08:12:38 -0700 | [diff] [blame] | 177 | pmbus_do_remove(client); | 
| Guenter Roeck | 84fc458 | 2011-03-06 15:54:27 -0800 | [diff] [blame] | 178 | kfree(info); | 
| Guenter Roeck | 866cf12 | 2011-08-26 08:12:38 -0700 | [diff] [blame] | 179 | return 0; | 
| Guenter Roeck | 84fc458 | 2011-03-06 15:54:27 -0800 | [diff] [blame] | 180 | } | 
|  | 181 |  | 
|  | 182 |  | 
|  | 183 | /* This is the driver that will be inserted */ | 
|  | 184 | static struct i2c_driver ucd9200_driver = { | 
|  | 185 | .driver = { | 
|  | 186 | .name = "ucd9200", | 
|  | 187 | }, | 
|  | 188 | .probe = ucd9200_probe, | 
|  | 189 | .remove = ucd9200_remove, | 
|  | 190 | .id_table = ucd9200_id, | 
|  | 191 | }; | 
|  | 192 |  | 
|  | 193 | static int __init ucd9200_init(void) | 
|  | 194 | { | 
|  | 195 | return i2c_add_driver(&ucd9200_driver); | 
|  | 196 | } | 
|  | 197 |  | 
|  | 198 | static void __exit ucd9200_exit(void) | 
|  | 199 | { | 
|  | 200 | i2c_del_driver(&ucd9200_driver); | 
|  | 201 | } | 
|  | 202 |  | 
|  | 203 | MODULE_AUTHOR("Guenter Roeck"); | 
|  | 204 | MODULE_DESCRIPTION("PMBus driver for TI UCD922x, UCD924x"); | 
|  | 205 | MODULE_LICENSE("GPL"); | 
|  | 206 | module_init(ucd9200_init); | 
|  | 207 | module_exit(ucd9200_exit); |