1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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/mutex.h>
27#include <linux/i2c.h>
28#include "pmbus.h"
29
30
31
32
33static void pmbus_find_sensor_groups(struct i2c_client *client,
34 struct pmbus_driver_info *info)
35{
36 int page;
37
38
39 if (pmbus_check_word_register(client, 0, PMBUS_READ_VIN))
40 info->func[0] |= PMBUS_HAVE_VIN;
41 if (pmbus_check_word_register(client, 0, PMBUS_READ_VCAP))
42 info->func[0] |= PMBUS_HAVE_VCAP;
43 if (pmbus_check_word_register(client, 0, PMBUS_READ_IIN))
44 info->func[0] |= PMBUS_HAVE_IIN;
45 if (pmbus_check_word_register(client, 0, PMBUS_READ_PIN))
46 info->func[0] |= PMBUS_HAVE_PIN;
47 if (info->func[0]
48 && pmbus_check_byte_register(client, 0, PMBUS_STATUS_INPUT))
49 info->func[0] |= PMBUS_HAVE_STATUS_INPUT;
50 if (pmbus_check_byte_register(client, 0, PMBUS_FAN_CONFIG_12) &&
51 pmbus_check_word_register(client, 0, PMBUS_READ_FAN_SPEED_1)) {
52 info->func[0] |= PMBUS_HAVE_FAN12;
53 if (pmbus_check_byte_register(client, 0, PMBUS_STATUS_FAN_12))
54 info->func[0] |= PMBUS_HAVE_STATUS_FAN12;
55 }
56 if (pmbus_check_byte_register(client, 0, PMBUS_FAN_CONFIG_34) &&
57 pmbus_check_word_register(client, 0, PMBUS_READ_FAN_SPEED_3)) {
58 info->func[0] |= PMBUS_HAVE_FAN34;
59 if (pmbus_check_byte_register(client, 0, PMBUS_STATUS_FAN_34))
60 info->func[0] |= PMBUS_HAVE_STATUS_FAN34;
61 }
62 if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_1))
63 info->func[0] |= PMBUS_HAVE_TEMP;
64 if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_2))
65 info->func[0] |= PMBUS_HAVE_TEMP2;
66 if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_3))
67 info->func[0] |= PMBUS_HAVE_TEMP3;
68 if (info->func[0] & (PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2
69 | PMBUS_HAVE_TEMP3)
70 && pmbus_check_byte_register(client, 0,
71 PMBUS_STATUS_TEMPERATURE))
72 info->func[0] |= PMBUS_HAVE_STATUS_TEMP;
73
74
75 for (page = 0; page < info->pages; page++) {
76 if (pmbus_check_word_register(client, page, PMBUS_READ_VOUT)) {
77 info->func[page] |= PMBUS_HAVE_VOUT;
78 if (pmbus_check_byte_register(client, page,
79 PMBUS_STATUS_VOUT))
80 info->func[page] |= PMBUS_HAVE_STATUS_VOUT;
81 }
82 if (pmbus_check_word_register(client, page, PMBUS_READ_IOUT)) {
83 info->func[page] |= PMBUS_HAVE_IOUT;
84 if (pmbus_check_byte_register(client, 0,
85 PMBUS_STATUS_IOUT))
86 info->func[page] |= PMBUS_HAVE_STATUS_IOUT;
87 }
88 if (pmbus_check_word_register(client, page, PMBUS_READ_POUT))
89 info->func[page] |= PMBUS_HAVE_POUT;
90 }
91}
92
93
94
95
96static int pmbus_identify(struct i2c_client *client,
97 struct pmbus_driver_info *info)
98{
99 int ret = 0;
100
101 if (!info->pages) {
102
103
104
105
106
107
108 if (pmbus_check_byte_register(client, 0, PMBUS_PAGE)) {
109 int page;
110
111 for (page = 1; page < PMBUS_PAGES; page++) {
112 if (pmbus_set_page(client, page) < 0)
113 break;
114 }
115 pmbus_set_page(client, 0);
116 info->pages = page;
117 } else {
118 info->pages = 1;
119 }
120 }
121
122 if (pmbus_check_byte_register(client, 0, PMBUS_VOUT_MODE)) {
123 int vout_mode;
124
125 vout_mode = pmbus_read_byte_data(client, 0, PMBUS_VOUT_MODE);
126 if (vout_mode >= 0 && vout_mode != 0xff) {
127 switch (vout_mode >> 5) {
128 case 0:
129 break;
130 case 1:
131 info->format[PSC_VOLTAGE_OUT] = vid;
132 break;
133 case 2:
134 info->format[PSC_VOLTAGE_OUT] = direct;
135 break;
136 default:
137 ret = -ENODEV;
138 goto abort;
139 }
140 }
141 }
142
143
144
145
146
147
148
149
150
151
152
153
154 if (info->format[PSC_VOLTAGE_OUT] == direct) {
155 ret = -ENODEV;
156 goto abort;
157 }
158
159
160 pmbus_find_sensor_groups(client, info);
161abort:
162 return ret;
163}
164
165static int pmbus_probe(struct i2c_client *client,
166 const struct i2c_device_id *id)
167{
168 struct pmbus_driver_info *info;
169
170 info = devm_kzalloc(&client->dev, sizeof(struct pmbus_driver_info),
171 GFP_KERNEL);
172 if (!info)
173 return -ENOMEM;
174
175 info->pages = id->driver_data;
176 info->identify = pmbus_identify;
177
178 return pmbus_do_probe(client, id, info);
179}
180
181
182
183
184static const struct i2c_device_id pmbus_id[] = {
185 {"adp4000", 1},
186 {"bmr453", 1},
187 {"bmr454", 1},
188 {"mdt040", 1},
189 {"ncp4200", 1},
190 {"ncp4208", 1},
191 {"pdt003", 1},
192 {"pdt006", 1},
193 {"pdt012", 1},
194 {"pmbus", 0},
195 {"tps40400", 1},
196 {"tps40422", 2},
197 {"udt020", 1},
198 {}
199};
200
201MODULE_DEVICE_TABLE(i2c, pmbus_id);
202
203
204static struct i2c_driver pmbus_driver = {
205 .driver = {
206 .name = "pmbus",
207 },
208 .probe = pmbus_probe,
209 .remove = pmbus_do_remove,
210 .id_table = pmbus_id,
211};
212
213module_i2c_driver(pmbus_driver);
214
215MODULE_AUTHOR("Guenter Roeck");
216MODULE_DESCRIPTION("Generic PMBus driver");
217MODULE_LICENSE("GPL");
218