Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Hardware monitoring driver for Analog Devices ADM1275 Hot-Swap Controller |
| 3 | * and Digital Power Monitor |
| 4 | * |
| 5 | * Copyright (c) 2011 Ericsson AB. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | */ |
| 17 | |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/err.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/i2c.h> |
| 24 | #include "pmbus.h" |
| 25 | |
Guenter Roeck | c576e30 | 2011-07-09 11:17:33 -0700 | [diff] [blame] | 26 | #define ADM1275_PEAK_IOUT 0xd0 |
| 27 | #define ADM1275_PEAK_VIN 0xd1 |
| 28 | #define ADM1275_PEAK_VOUT 0xd2 |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 29 | #define ADM1275_PMON_CONFIG 0xd4 |
| 30 | |
| 31 | #define ADM1275_VIN_VOUT_SELECT (1 << 6) |
| 32 | #define ADM1275_VRANGE (1 << 5) |
| 33 | |
Guenter Roeck | c576e30 | 2011-07-09 11:17:33 -0700 | [diff] [blame] | 34 | static int adm1275_read_word_data(struct i2c_client *client, int page, int reg) |
| 35 | { |
| 36 | int ret; |
| 37 | |
| 38 | if (page) |
| 39 | return -EINVAL; |
| 40 | |
| 41 | switch (reg) { |
| 42 | case PMBUS_VIRT_READ_IOUT_MAX: |
| 43 | ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_IOUT); |
| 44 | break; |
| 45 | case PMBUS_VIRT_READ_VOUT_MAX: |
| 46 | ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_VOUT); |
| 47 | break; |
| 48 | case PMBUS_VIRT_READ_VIN_MAX: |
| 49 | ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_VIN); |
| 50 | break; |
| 51 | case PMBUS_VIRT_RESET_IOUT_HISTORY: |
| 52 | case PMBUS_VIRT_RESET_VOUT_HISTORY: |
| 53 | case PMBUS_VIRT_RESET_VIN_HISTORY: |
| 54 | ret = 0; |
| 55 | break; |
| 56 | default: |
| 57 | ret = -ENODATA; |
| 58 | break; |
| 59 | } |
| 60 | return ret; |
| 61 | } |
| 62 | |
| 63 | static int adm1275_write_word_data(struct i2c_client *client, int page, int reg, |
| 64 | u16 word) |
| 65 | { |
| 66 | int ret; |
| 67 | |
| 68 | if (page) |
| 69 | return -EINVAL; |
| 70 | |
| 71 | switch (reg) { |
| 72 | case PMBUS_VIRT_RESET_IOUT_HISTORY: |
| 73 | ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_IOUT, 0); |
| 74 | break; |
| 75 | case PMBUS_VIRT_RESET_VOUT_HISTORY: |
| 76 | ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VOUT, 0); |
| 77 | break; |
| 78 | case PMBUS_VIRT_RESET_VIN_HISTORY: |
| 79 | ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VIN, 0); |
| 80 | break; |
| 81 | default: |
| 82 | ret = -ENODATA; |
| 83 | break; |
| 84 | } |
| 85 | return ret; |
| 86 | } |
| 87 | |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 88 | static int adm1275_probe(struct i2c_client *client, |
| 89 | const struct i2c_device_id *id) |
| 90 | { |
| 91 | int config; |
Guenter Roeck | 3b33ca4 | 2011-06-30 02:30:03 -0700 | [diff] [blame] | 92 | int ret; |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 93 | struct pmbus_driver_info *info; |
| 94 | |
| 95 | if (!i2c_check_functionality(client->adapter, |
| 96 | I2C_FUNC_SMBUS_READ_BYTE_DATA)) |
| 97 | return -ENODEV; |
| 98 | |
| 99 | info = kzalloc(sizeof(struct pmbus_driver_info), GFP_KERNEL); |
| 100 | if (!info) |
| 101 | return -ENOMEM; |
| 102 | |
| 103 | config = i2c_smbus_read_byte_data(client, ADM1275_PMON_CONFIG); |
Guenter Roeck | 3b33ca4 | 2011-06-30 02:30:03 -0700 | [diff] [blame] | 104 | if (config < 0) { |
| 105 | ret = config; |
| 106 | goto err_mem; |
| 107 | } |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 108 | |
| 109 | info->pages = 1; |
Guenter Roeck | 1061d85 | 2011-06-25 11:21:49 -0700 | [diff] [blame] | 110 | info->format[PSC_VOLTAGE_IN] = direct; |
| 111 | info->format[PSC_VOLTAGE_OUT] = direct; |
| 112 | info->format[PSC_CURRENT_OUT] = direct; |
Guenter Roeck | 7e97bbb | 2011-07-14 14:18:03 -0700 | [diff] [blame] | 113 | info->m[PSC_CURRENT_OUT] = 807; |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 114 | info->b[PSC_CURRENT_OUT] = 20475; |
| 115 | info->R[PSC_CURRENT_OUT] = -1; |
| 116 | info->func[0] = PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT; |
| 117 | |
Guenter Roeck | c576e30 | 2011-07-09 11:17:33 -0700 | [diff] [blame] | 118 | info->read_word_data = adm1275_read_word_data; |
| 119 | info->write_word_data = adm1275_write_word_data; |
| 120 | |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 121 | if (config & ADM1275_VRANGE) { |
Guenter Roeck | 7e97bbb | 2011-07-14 14:18:03 -0700 | [diff] [blame] | 122 | info->m[PSC_VOLTAGE_IN] = 19199; |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 123 | info->b[PSC_VOLTAGE_IN] = 0; |
| 124 | info->R[PSC_VOLTAGE_IN] = -2; |
Guenter Roeck | 7e97bbb | 2011-07-14 14:18:03 -0700 | [diff] [blame] | 125 | info->m[PSC_VOLTAGE_OUT] = 19199; |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 126 | info->b[PSC_VOLTAGE_OUT] = 0; |
| 127 | info->R[PSC_VOLTAGE_OUT] = -2; |
| 128 | } else { |
Guenter Roeck | 7e97bbb | 2011-07-14 14:18:03 -0700 | [diff] [blame] | 129 | info->m[PSC_VOLTAGE_IN] = 6720; |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 130 | info->b[PSC_VOLTAGE_IN] = 0; |
| 131 | info->R[PSC_VOLTAGE_IN] = -1; |
Guenter Roeck | 7e97bbb | 2011-07-14 14:18:03 -0700 | [diff] [blame] | 132 | info->m[PSC_VOLTAGE_OUT] = 6720; |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 133 | info->b[PSC_VOLTAGE_OUT] = 0; |
| 134 | info->R[PSC_VOLTAGE_OUT] = -1; |
| 135 | } |
| 136 | |
| 137 | if (config & ADM1275_VIN_VOUT_SELECT) |
| 138 | info->func[0] |= PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT; |
| 139 | else |
| 140 | info->func[0] |= PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT; |
| 141 | |
Guenter Roeck | 3b33ca4 | 2011-06-30 02:30:03 -0700 | [diff] [blame] | 142 | ret = pmbus_do_probe(client, id, info); |
| 143 | if (ret) |
| 144 | goto err_mem; |
| 145 | return 0; |
| 146 | |
| 147 | err_mem: |
| 148 | kfree(info); |
| 149 | return ret; |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | static int adm1275_remove(struct i2c_client *client) |
| 153 | { |
| 154 | const struct pmbus_driver_info *info = pmbus_get_driver_info(client); |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 155 | |
Guenter Roeck | 866cf12 | 2011-08-26 08:12:38 -0700 | [diff] [blame^] | 156 | pmbus_do_remove(client); |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 157 | kfree(info); |
Guenter Roeck | 866cf12 | 2011-08-26 08:12:38 -0700 | [diff] [blame^] | 158 | return 0; |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | static const struct i2c_device_id adm1275_id[] = { |
| 162 | {"adm1275", 0}, |
| 163 | { } |
| 164 | }; |
| 165 | MODULE_DEVICE_TABLE(i2c, adm1275_id); |
| 166 | |
| 167 | static struct i2c_driver adm1275_driver = { |
| 168 | .driver = { |
| 169 | .name = "adm1275", |
| 170 | }, |
| 171 | .probe = adm1275_probe, |
| 172 | .remove = adm1275_remove, |
| 173 | .id_table = adm1275_id, |
| 174 | }; |
| 175 | |
| 176 | static int __init adm1275_init(void) |
| 177 | { |
| 178 | return i2c_add_driver(&adm1275_driver); |
| 179 | } |
| 180 | |
| 181 | static void __exit adm1275_exit(void) |
| 182 | { |
| 183 | i2c_del_driver(&adm1275_driver); |
| 184 | } |
| 185 | |
| 186 | MODULE_AUTHOR("Guenter Roeck"); |
| 187 | MODULE_DESCRIPTION("PMBus driver for Analog Devices ADM1275"); |
| 188 | MODULE_LICENSE("GPL"); |
| 189 | module_init(adm1275_init); |
| 190 | module_exit(adm1275_exit); |