Thomas Gleixner | 74ba920 | 2019-05-20 09:19:02 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Guenter Roeck | 8ea3238 | 2011-01-26 20:11:04 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Hardware monitoring driver for Maxim MAX8688 |
| 4 | * |
| 5 | * Copyright (c) 2011 Ericsson AB. |
Guenter Roeck | 8ea3238 | 2011-01-26 20:11:04 -0800 | [diff] [blame] | 6 | */ |
| 7 | |
Guenter Roeck | 2c052d42 | 2015-08-17 16:17:24 -0700 | [diff] [blame] | 8 | #include <linux/bitops.h> |
Guenter Roeck | 8ea3238 | 2011-01-26 20:11:04 -0800 | [diff] [blame] | 9 | #include <linux/kernel.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/err.h> |
| 13 | #include <linux/i2c.h> |
| 14 | #include "pmbus.h" |
| 15 | |
Guenter Roeck | 70e94b2 | 2011-07-09 13:11:44 -0700 | [diff] [blame] | 16 | #define MAX8688_MFR_VOUT_PEAK 0xd4 |
| 17 | #define MAX8688_MFR_IOUT_PEAK 0xd5 |
| 18 | #define MAX8688_MFR_TEMPERATURE_PEAK 0xd6 |
Guenter Roeck | 8ea3238 | 2011-01-26 20:11:04 -0800 | [diff] [blame] | 19 | #define MAX8688_MFG_STATUS 0xd8 |
| 20 | |
Guenter Roeck | 2c052d42 | 2015-08-17 16:17:24 -0700 | [diff] [blame] | 21 | #define MAX8688_STATUS_OC_FAULT BIT(4) |
| 22 | #define MAX8688_STATUS_OV_FAULT BIT(5) |
| 23 | #define MAX8688_STATUS_OV_WARNING BIT(8) |
| 24 | #define MAX8688_STATUS_UV_FAULT BIT(9) |
| 25 | #define MAX8688_STATUS_UV_WARNING BIT(10) |
| 26 | #define MAX8688_STATUS_UC_FAULT BIT(11) |
| 27 | #define MAX8688_STATUS_OC_WARNING BIT(12) |
| 28 | #define MAX8688_STATUS_OT_FAULT BIT(13) |
| 29 | #define MAX8688_STATUS_OT_WARNING BIT(14) |
Guenter Roeck | 8ea3238 | 2011-01-26 20:11:04 -0800 | [diff] [blame] | 30 | |
Guenter Roeck | 70e94b2 | 2011-07-09 13:11:44 -0700 | [diff] [blame] | 31 | static int max8688_read_word_data(struct i2c_client *client, int page, int reg) |
| 32 | { |
| 33 | int ret; |
| 34 | |
Guenter Roeck | a46f8cd | 2018-03-10 17:49:47 -0800 | [diff] [blame] | 35 | if (page > 0) |
Guenter Roeck | 179144a | 2011-09-01 08:34:31 -0700 | [diff] [blame] | 36 | return -ENXIO; |
Guenter Roeck | 70e94b2 | 2011-07-09 13:11:44 -0700 | [diff] [blame] | 37 | |
| 38 | switch (reg) { |
| 39 | case PMBUS_VIRT_READ_VOUT_MAX: |
| 40 | ret = pmbus_read_word_data(client, 0, MAX8688_MFR_VOUT_PEAK); |
| 41 | break; |
| 42 | case PMBUS_VIRT_READ_IOUT_MAX: |
| 43 | ret = pmbus_read_word_data(client, 0, MAX8688_MFR_IOUT_PEAK); |
| 44 | break; |
| 45 | case PMBUS_VIRT_READ_TEMP_MAX: |
| 46 | ret = pmbus_read_word_data(client, 0, |
| 47 | MAX8688_MFR_TEMPERATURE_PEAK); |
| 48 | break; |
| 49 | case PMBUS_VIRT_RESET_VOUT_HISTORY: |
| 50 | case PMBUS_VIRT_RESET_IOUT_HISTORY: |
| 51 | case PMBUS_VIRT_RESET_TEMP_HISTORY: |
| 52 | ret = 0; |
| 53 | break; |
| 54 | default: |
| 55 | ret = -ENODATA; |
| 56 | break; |
| 57 | } |
| 58 | return ret; |
| 59 | } |
| 60 | |
| 61 | static int max8688_write_word_data(struct i2c_client *client, int page, int reg, |
| 62 | u16 word) |
| 63 | { |
| 64 | int ret; |
| 65 | |
| 66 | switch (reg) { |
| 67 | case PMBUS_VIRT_RESET_VOUT_HISTORY: |
| 68 | ret = pmbus_write_word_data(client, 0, MAX8688_MFR_VOUT_PEAK, |
| 69 | 0); |
| 70 | break; |
| 71 | case PMBUS_VIRT_RESET_IOUT_HISTORY: |
| 72 | ret = pmbus_write_word_data(client, 0, MAX8688_MFR_IOUT_PEAK, |
| 73 | 0); |
| 74 | break; |
| 75 | case PMBUS_VIRT_RESET_TEMP_HISTORY: |
| 76 | ret = pmbus_write_word_data(client, 0, |
| 77 | MAX8688_MFR_TEMPERATURE_PEAK, |
| 78 | 0xffff); |
| 79 | break; |
| 80 | default: |
| 81 | ret = -ENODATA; |
| 82 | break; |
| 83 | } |
| 84 | return ret; |
| 85 | } |
| 86 | |
Guenter Roeck | 2cfa6ae | 2011-03-08 23:00:10 -0800 | [diff] [blame] | 87 | static int max8688_read_byte_data(struct i2c_client *client, int page, int reg) |
Guenter Roeck | 8ea3238 | 2011-01-26 20:11:04 -0800 | [diff] [blame] | 88 | { |
| 89 | int ret = 0; |
| 90 | int mfg_status; |
| 91 | |
Guenter Roeck | da8e48a | 2011-07-29 22:19:39 -0700 | [diff] [blame] | 92 | if (page > 0) |
Guenter Roeck | 179144a | 2011-09-01 08:34:31 -0700 | [diff] [blame] | 93 | return -ENXIO; |
Guenter Roeck | 8ea3238 | 2011-01-26 20:11:04 -0800 | [diff] [blame] | 94 | |
| 95 | switch (reg) { |
| 96 | case PMBUS_STATUS_VOUT: |
| 97 | mfg_status = pmbus_read_word_data(client, 0, |
| 98 | MAX8688_MFG_STATUS); |
| 99 | if (mfg_status < 0) |
| 100 | return mfg_status; |
| 101 | if (mfg_status & MAX8688_STATUS_UV_WARNING) |
| 102 | ret |= PB_VOLTAGE_UV_WARNING; |
| 103 | if (mfg_status & MAX8688_STATUS_UV_FAULT) |
| 104 | ret |= PB_VOLTAGE_UV_FAULT; |
| 105 | if (mfg_status & MAX8688_STATUS_OV_WARNING) |
| 106 | ret |= PB_VOLTAGE_OV_WARNING; |
| 107 | if (mfg_status & MAX8688_STATUS_OV_FAULT) |
| 108 | ret |= PB_VOLTAGE_OV_FAULT; |
| 109 | break; |
| 110 | case PMBUS_STATUS_IOUT: |
| 111 | mfg_status = pmbus_read_word_data(client, 0, |
| 112 | MAX8688_MFG_STATUS); |
| 113 | if (mfg_status < 0) |
| 114 | return mfg_status; |
| 115 | if (mfg_status & MAX8688_STATUS_UC_FAULT) |
| 116 | ret |= PB_IOUT_UC_FAULT; |
| 117 | if (mfg_status & MAX8688_STATUS_OC_WARNING) |
| 118 | ret |= PB_IOUT_OC_WARNING; |
| 119 | if (mfg_status & MAX8688_STATUS_OC_FAULT) |
| 120 | ret |= PB_IOUT_OC_FAULT; |
| 121 | break; |
| 122 | case PMBUS_STATUS_TEMPERATURE: |
| 123 | mfg_status = pmbus_read_word_data(client, 0, |
| 124 | MAX8688_MFG_STATUS); |
| 125 | if (mfg_status < 0) |
| 126 | return mfg_status; |
| 127 | if (mfg_status & MAX8688_STATUS_OT_WARNING) |
| 128 | ret |= PB_TEMP_OT_WARNING; |
| 129 | if (mfg_status & MAX8688_STATUS_OT_FAULT) |
| 130 | ret |= PB_TEMP_OT_FAULT; |
| 131 | break; |
| 132 | default: |
| 133 | ret = -ENODATA; |
| 134 | break; |
| 135 | } |
| 136 | return ret; |
| 137 | } |
| 138 | |
| 139 | static struct pmbus_driver_info max8688_info = { |
| 140 | .pages = 1, |
Guenter Roeck | 1061d85 | 2011-06-25 11:21:49 -0700 | [diff] [blame] | 141 | .format[PSC_VOLTAGE_IN] = direct, |
| 142 | .format[PSC_VOLTAGE_OUT] = direct, |
| 143 | .format[PSC_TEMPERATURE] = direct, |
| 144 | .format[PSC_CURRENT_OUT] = direct, |
Guenter Roeck | 8ea3238 | 2011-01-26 20:11:04 -0800 | [diff] [blame] | 145 | .m[PSC_VOLTAGE_IN] = 19995, |
| 146 | .b[PSC_VOLTAGE_IN] = 0, |
| 147 | .R[PSC_VOLTAGE_IN] = -1, |
| 148 | .m[PSC_VOLTAGE_OUT] = 19995, |
| 149 | .b[PSC_VOLTAGE_OUT] = 0, |
| 150 | .R[PSC_VOLTAGE_OUT] = -1, |
| 151 | .m[PSC_CURRENT_OUT] = 23109, |
| 152 | .b[PSC_CURRENT_OUT] = 0, |
| 153 | .R[PSC_CURRENT_OUT] = -2, |
| 154 | .m[PSC_TEMPERATURE] = -7612, |
| 155 | .b[PSC_TEMPERATURE] = 335, |
| 156 | .R[PSC_TEMPERATURE] = -3, |
| 157 | .func[0] = PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT | PMBUS_HAVE_TEMP |
| 158 | | PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_STATUS_IOUT |
| 159 | | PMBUS_HAVE_STATUS_TEMP, |
Guenter Roeck | 2cfa6ae | 2011-03-08 23:00:10 -0800 | [diff] [blame] | 160 | .read_byte_data = max8688_read_byte_data, |
Guenter Roeck | 70e94b2 | 2011-07-09 13:11:44 -0700 | [diff] [blame] | 161 | .read_word_data = max8688_read_word_data, |
| 162 | .write_word_data = max8688_write_word_data, |
Guenter Roeck | 8ea3238 | 2011-01-26 20:11:04 -0800 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | static int max8688_probe(struct i2c_client *client, |
| 166 | const struct i2c_device_id *id) |
| 167 | { |
| 168 | return pmbus_do_probe(client, id, &max8688_info); |
| 169 | } |
| 170 | |
Guenter Roeck | 8ea3238 | 2011-01-26 20:11:04 -0800 | [diff] [blame] | 171 | static const struct i2c_device_id max8688_id[] = { |
| 172 | {"max8688", 0}, |
| 173 | { } |
| 174 | }; |
| 175 | |
| 176 | MODULE_DEVICE_TABLE(i2c, max8688_id); |
| 177 | |
| 178 | /* This is the driver that will be inserted */ |
| 179 | static struct i2c_driver max8688_driver = { |
| 180 | .driver = { |
| 181 | .name = "max8688", |
| 182 | }, |
| 183 | .probe = max8688_probe, |
Guenter Roeck | dd285ad | 2012-02-22 08:56:44 -0800 | [diff] [blame] | 184 | .remove = pmbus_do_remove, |
Guenter Roeck | 8ea3238 | 2011-01-26 20:11:04 -0800 | [diff] [blame] | 185 | .id_table = max8688_id, |
| 186 | }; |
| 187 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 188 | module_i2c_driver(max8688_driver); |
Guenter Roeck | 8ea3238 | 2011-01-26 20:11:04 -0800 | [diff] [blame] | 189 | |
| 190 | MODULE_AUTHOR("Guenter Roeck"); |
| 191 | MODULE_DESCRIPTION("PMBus driver for Maxim MAX8688"); |
| 192 | MODULE_LICENSE("GPL"); |