blob: 643ccfc051067fac399599d0a2360202319fcb04 [file] [log] [blame]
Thomas Gleixner74ba9202019-05-20 09:19:02 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Guenter Roeck8ea32382011-01-26 20:11:04 -08002/*
3 * Hardware monitoring driver for Maxim MAX8688
4 *
5 * Copyright (c) 2011 Ericsson AB.
Guenter Roeck8ea32382011-01-26 20:11:04 -08006 */
7
Guenter Roeck2c052d422015-08-17 16:17:24 -07008#include <linux/bitops.h>
Guenter Roeck8ea32382011-01-26 20:11:04 -08009#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 Roeck70e94b22011-07-09 13:11:44 -070016#define MAX8688_MFR_VOUT_PEAK 0xd4
17#define MAX8688_MFR_IOUT_PEAK 0xd5
18#define MAX8688_MFR_TEMPERATURE_PEAK 0xd6
Guenter Roeck8ea32382011-01-26 20:11:04 -080019#define MAX8688_MFG_STATUS 0xd8
20
Guenter Roeck2c052d422015-08-17 16:17:24 -070021#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 Roeck8ea32382011-01-26 20:11:04 -080030
Guenter Roeck43f33b62020-01-14 09:49:27 -080031static int max8688_read_word_data(struct i2c_client *client, int page,
32 int phase, int reg)
Guenter Roeck70e94b22011-07-09 13:11:44 -070033{
34 int ret;
35
Guenter Roecka46f8cd2018-03-10 17:49:47 -080036 if (page > 0)
Guenter Roeck179144a2011-09-01 08:34:31 -070037 return -ENXIO;
Guenter Roeck70e94b22011-07-09 13:11:44 -070038
39 switch (reg) {
40 case PMBUS_VIRT_READ_VOUT_MAX:
Guenter Roeck43f33b62020-01-14 09:49:27 -080041 ret = pmbus_read_word_data(client, 0, 0xff,
42 MAX8688_MFR_VOUT_PEAK);
Guenter Roeck70e94b22011-07-09 13:11:44 -070043 break;
44 case PMBUS_VIRT_READ_IOUT_MAX:
Guenter Roeck43f33b62020-01-14 09:49:27 -080045 ret = pmbus_read_word_data(client, 0, 0xff,
46 MAX8688_MFR_IOUT_PEAK);
Guenter Roeck70e94b22011-07-09 13:11:44 -070047 break;
48 case PMBUS_VIRT_READ_TEMP_MAX:
Guenter Roeck43f33b62020-01-14 09:49:27 -080049 ret = pmbus_read_word_data(client, 0, 0xff,
Guenter Roeck70e94b22011-07-09 13:11:44 -070050 MAX8688_MFR_TEMPERATURE_PEAK);
51 break;
52 case PMBUS_VIRT_RESET_VOUT_HISTORY:
53 case PMBUS_VIRT_RESET_IOUT_HISTORY:
54 case PMBUS_VIRT_RESET_TEMP_HISTORY:
55 ret = 0;
56 break;
57 default:
58 ret = -ENODATA;
59 break;
60 }
61 return ret;
62}
63
64static int max8688_write_word_data(struct i2c_client *client, int page, int reg,
65 u16 word)
66{
67 int ret;
68
69 switch (reg) {
70 case PMBUS_VIRT_RESET_VOUT_HISTORY:
71 ret = pmbus_write_word_data(client, 0, MAX8688_MFR_VOUT_PEAK,
72 0);
73 break;
74 case PMBUS_VIRT_RESET_IOUT_HISTORY:
75 ret = pmbus_write_word_data(client, 0, MAX8688_MFR_IOUT_PEAK,
76 0);
77 break;
78 case PMBUS_VIRT_RESET_TEMP_HISTORY:
79 ret = pmbus_write_word_data(client, 0,
80 MAX8688_MFR_TEMPERATURE_PEAK,
81 0xffff);
82 break;
83 default:
84 ret = -ENODATA;
85 break;
86 }
87 return ret;
88}
89
Guenter Roeck2cfa6ae2011-03-08 23:00:10 -080090static int max8688_read_byte_data(struct i2c_client *client, int page, int reg)
Guenter Roeck8ea32382011-01-26 20:11:04 -080091{
92 int ret = 0;
93 int mfg_status;
94
Guenter Roeckda8e48a2011-07-29 22:19:39 -070095 if (page > 0)
Guenter Roeck179144a2011-09-01 08:34:31 -070096 return -ENXIO;
Guenter Roeck8ea32382011-01-26 20:11:04 -080097
98 switch (reg) {
99 case PMBUS_STATUS_VOUT:
Guenter Roeck43f33b62020-01-14 09:49:27 -0800100 mfg_status = pmbus_read_word_data(client, 0, 0xff,
Guenter Roeck8ea32382011-01-26 20:11:04 -0800101 MAX8688_MFG_STATUS);
102 if (mfg_status < 0)
103 return mfg_status;
104 if (mfg_status & MAX8688_STATUS_UV_WARNING)
105 ret |= PB_VOLTAGE_UV_WARNING;
106 if (mfg_status & MAX8688_STATUS_UV_FAULT)
107 ret |= PB_VOLTAGE_UV_FAULT;
108 if (mfg_status & MAX8688_STATUS_OV_WARNING)
109 ret |= PB_VOLTAGE_OV_WARNING;
110 if (mfg_status & MAX8688_STATUS_OV_FAULT)
111 ret |= PB_VOLTAGE_OV_FAULT;
112 break;
113 case PMBUS_STATUS_IOUT:
Guenter Roeck43f33b62020-01-14 09:49:27 -0800114 mfg_status = pmbus_read_word_data(client, 0, 0xff,
Guenter Roeck8ea32382011-01-26 20:11:04 -0800115 MAX8688_MFG_STATUS);
116 if (mfg_status < 0)
117 return mfg_status;
118 if (mfg_status & MAX8688_STATUS_UC_FAULT)
119 ret |= PB_IOUT_UC_FAULT;
120 if (mfg_status & MAX8688_STATUS_OC_WARNING)
121 ret |= PB_IOUT_OC_WARNING;
122 if (mfg_status & MAX8688_STATUS_OC_FAULT)
123 ret |= PB_IOUT_OC_FAULT;
124 break;
125 case PMBUS_STATUS_TEMPERATURE:
Guenter Roeck43f33b62020-01-14 09:49:27 -0800126 mfg_status = pmbus_read_word_data(client, 0, 0xff,
Guenter Roeck8ea32382011-01-26 20:11:04 -0800127 MAX8688_MFG_STATUS);
128 if (mfg_status < 0)
129 return mfg_status;
130 if (mfg_status & MAX8688_STATUS_OT_WARNING)
131 ret |= PB_TEMP_OT_WARNING;
132 if (mfg_status & MAX8688_STATUS_OT_FAULT)
133 ret |= PB_TEMP_OT_FAULT;
134 break;
135 default:
136 ret = -ENODATA;
137 break;
138 }
139 return ret;
140}
141
142static struct pmbus_driver_info max8688_info = {
143 .pages = 1,
Guenter Roeck1061d852011-06-25 11:21:49 -0700144 .format[PSC_VOLTAGE_IN] = direct,
145 .format[PSC_VOLTAGE_OUT] = direct,
146 .format[PSC_TEMPERATURE] = direct,
147 .format[PSC_CURRENT_OUT] = direct,
Guenter Roeck8ea32382011-01-26 20:11:04 -0800148 .m[PSC_VOLTAGE_IN] = 19995,
149 .b[PSC_VOLTAGE_IN] = 0,
150 .R[PSC_VOLTAGE_IN] = -1,
151 .m[PSC_VOLTAGE_OUT] = 19995,
152 .b[PSC_VOLTAGE_OUT] = 0,
153 .R[PSC_VOLTAGE_OUT] = -1,
154 .m[PSC_CURRENT_OUT] = 23109,
155 .b[PSC_CURRENT_OUT] = 0,
156 .R[PSC_CURRENT_OUT] = -2,
157 .m[PSC_TEMPERATURE] = -7612,
158 .b[PSC_TEMPERATURE] = 335,
159 .R[PSC_TEMPERATURE] = -3,
160 .func[0] = PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT | PMBUS_HAVE_TEMP
161 | PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_STATUS_IOUT
162 | PMBUS_HAVE_STATUS_TEMP,
Guenter Roeck2cfa6ae2011-03-08 23:00:10 -0800163 .read_byte_data = max8688_read_byte_data,
Guenter Roeck70e94b22011-07-09 13:11:44 -0700164 .read_word_data = max8688_read_word_data,
165 .write_word_data = max8688_write_word_data,
Guenter Roeck8ea32382011-01-26 20:11:04 -0800166};
167
168static int max8688_probe(struct i2c_client *client,
169 const struct i2c_device_id *id)
170{
171 return pmbus_do_probe(client, id, &max8688_info);
172}
173
Guenter Roeck8ea32382011-01-26 20:11:04 -0800174static const struct i2c_device_id max8688_id[] = {
175 {"max8688", 0},
176 { }
177};
178
179MODULE_DEVICE_TABLE(i2c, max8688_id);
180
181/* This is the driver that will be inserted */
182static struct i2c_driver max8688_driver = {
183 .driver = {
184 .name = "max8688",
185 },
186 .probe = max8688_probe,
Guenter Roeckdd285ad2012-02-22 08:56:44 -0800187 .remove = pmbus_do_remove,
Guenter Roeck8ea32382011-01-26 20:11:04 -0800188 .id_table = max8688_id,
189};
190
Axel Linf0967ee2012-01-20 15:38:18 +0800191module_i2c_driver(max8688_driver);
Guenter Roeck8ea32382011-01-26 20:11:04 -0800192
193MODULE_AUTHOR("Guenter Roeck");
194MODULE_DESCRIPTION("PMBus driver for Maxim MAX8688");
195MODULE_LICENSE("GPL");