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