hwmon: (aspeed-pwm-tacho) Deassert reset in probe
[linux-2.6-microblaze.git] / drivers / hwmon / pmbus / ibm-cffps.c
1 /*
2  * Copyright 2017 IBM Corp.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  */
9
10 #include <linux/bitops.h>
11 #include <linux/debugfs.h>
12 #include <linux/device.h>
13 #include <linux/fs.h>
14 #include <linux/i2c.h>
15 #include <linux/jiffies.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18
19 #include "pmbus.h"
20
21 #define CFFPS_FRU_CMD                           0x9A
22 #define CFFPS_PN_CMD                            0x9B
23 #define CFFPS_SN_CMD                            0x9E
24 #define CFFPS_CCIN_CMD                          0xBD
25 #define CFFPS_FW_CMD_START                      0xFA
26 #define CFFPS_FW_NUM_BYTES                      4
27
28 #define CFFPS_INPUT_HISTORY_CMD                 0xD6
29 #define CFFPS_INPUT_HISTORY_SIZE                100
30
31 /* STATUS_MFR_SPECIFIC bits */
32 #define CFFPS_MFR_FAN_FAULT                     BIT(0)
33 #define CFFPS_MFR_THERMAL_FAULT                 BIT(1)
34 #define CFFPS_MFR_OV_FAULT                      BIT(2)
35 #define CFFPS_MFR_UV_FAULT                      BIT(3)
36 #define CFFPS_MFR_PS_KILL                       BIT(4)
37 #define CFFPS_MFR_OC_FAULT                      BIT(5)
38 #define CFFPS_MFR_VAUX_FAULT                    BIT(6)
39 #define CFFPS_MFR_CURRENT_SHARE_WARNING         BIT(7)
40
41 enum {
42         CFFPS_DEBUGFS_INPUT_HISTORY = 0,
43         CFFPS_DEBUGFS_FRU,
44         CFFPS_DEBUGFS_PN,
45         CFFPS_DEBUGFS_SN,
46         CFFPS_DEBUGFS_CCIN,
47         CFFPS_DEBUGFS_FW,
48         CFFPS_DEBUGFS_NUM_ENTRIES
49 };
50
51 struct ibm_cffps_input_history {
52         struct mutex update_lock;
53         unsigned long last_update;
54
55         u8 byte_count;
56         u8 data[CFFPS_INPUT_HISTORY_SIZE];
57 };
58
59 struct ibm_cffps {
60         struct i2c_client *client;
61
62         struct ibm_cffps_input_history input_history;
63
64         int debugfs_entries[CFFPS_DEBUGFS_NUM_ENTRIES];
65 };
66
67 #define to_psu(x, y) container_of((x), struct ibm_cffps, debugfs_entries[(y)])
68
69 static ssize_t ibm_cffps_read_input_history(struct ibm_cffps *psu,
70                                             char __user *buf, size_t count,
71                                             loff_t *ppos)
72 {
73         int rc;
74         u8 msgbuf0[1] = { CFFPS_INPUT_HISTORY_CMD };
75         u8 msgbuf1[CFFPS_INPUT_HISTORY_SIZE + 1] = { 0 };
76         struct i2c_msg msg[2] = {
77                 {
78                         .addr = psu->client->addr,
79                         .flags = psu->client->flags,
80                         .len = 1,
81                         .buf = msgbuf0,
82                 }, {
83                         .addr = psu->client->addr,
84                         .flags = psu->client->flags | I2C_M_RD,
85                         .len = CFFPS_INPUT_HISTORY_SIZE + 1,
86                         .buf = msgbuf1,
87                 },
88         };
89
90         if (!*ppos) {
91                 mutex_lock(&psu->input_history.update_lock);
92                 if (time_after(jiffies, psu->input_history.last_update + HZ)) {
93                         /*
94                          * Use a raw i2c transfer, since we need more bytes
95                          * than Linux I2C supports through smbus xfr (only 32).
96                          */
97                         rc = i2c_transfer(psu->client->adapter, msg, 2);
98                         if (rc < 0) {
99                                 mutex_unlock(&psu->input_history.update_lock);
100                                 return rc;
101                         }
102
103                         psu->input_history.byte_count = msgbuf1[0];
104                         memcpy(psu->input_history.data, &msgbuf1[1],
105                                CFFPS_INPUT_HISTORY_SIZE);
106                         psu->input_history.last_update = jiffies;
107                 }
108
109                 mutex_unlock(&psu->input_history.update_lock);
110         }
111
112         return simple_read_from_buffer(buf, count, ppos,
113                                        psu->input_history.data,
114                                        psu->input_history.byte_count);
115 }
116
117 static ssize_t ibm_cffps_debugfs_op(struct file *file, char __user *buf,
118                                     size_t count, loff_t *ppos)
119 {
120         u8 cmd;
121         int i, rc;
122         int *idxp = file->private_data;
123         int idx = *idxp;
124         struct ibm_cffps *psu = to_psu(idxp, idx);
125         char data[I2C_SMBUS_BLOCK_MAX] = { 0 };
126
127         switch (idx) {
128         case CFFPS_DEBUGFS_INPUT_HISTORY:
129                 return ibm_cffps_read_input_history(psu, buf, count, ppos);
130         case CFFPS_DEBUGFS_FRU:
131                 cmd = CFFPS_FRU_CMD;
132                 break;
133         case CFFPS_DEBUGFS_PN:
134                 cmd = CFFPS_PN_CMD;
135                 break;
136         case CFFPS_DEBUGFS_SN:
137                 cmd = CFFPS_SN_CMD;
138                 break;
139         case CFFPS_DEBUGFS_CCIN:
140                 rc = i2c_smbus_read_word_swapped(psu->client, CFFPS_CCIN_CMD);
141                 if (rc < 0)
142                         return rc;
143
144                 rc = snprintf(data, 5, "%04X", rc);
145                 goto done;
146         case CFFPS_DEBUGFS_FW:
147                 for (i = 0; i < CFFPS_FW_NUM_BYTES; ++i) {
148                         rc = i2c_smbus_read_byte_data(psu->client,
149                                                       CFFPS_FW_CMD_START + i);
150                         if (rc < 0)
151                                 return rc;
152
153                         snprintf(&data[i * 2], 3, "%02X", rc);
154                 }
155
156                 rc = i * 2;
157                 goto done;
158         default:
159                 return -EINVAL;
160         }
161
162         rc = i2c_smbus_read_block_data(psu->client, cmd, data);
163         if (rc < 0)
164                 return rc;
165
166 done:
167         data[rc] = '\n';
168         rc += 2;
169
170         return simple_read_from_buffer(buf, count, ppos, data, rc);
171 }
172
173 static const struct file_operations ibm_cffps_fops = {
174         .llseek = noop_llseek,
175         .read = ibm_cffps_debugfs_op,
176         .open = simple_open,
177 };
178
179 static int ibm_cffps_read_byte_data(struct i2c_client *client, int page,
180                                     int reg)
181 {
182         int rc, mfr;
183
184         switch (reg) {
185         case PMBUS_STATUS_VOUT:
186         case PMBUS_STATUS_IOUT:
187         case PMBUS_STATUS_TEMPERATURE:
188         case PMBUS_STATUS_FAN_12:
189                 rc = pmbus_read_byte_data(client, page, reg);
190                 if (rc < 0)
191                         return rc;
192
193                 mfr = pmbus_read_byte_data(client, page,
194                                            PMBUS_STATUS_MFR_SPECIFIC);
195                 if (mfr < 0)
196                         /*
197                          * Return the status register instead of an error,
198                          * since we successfully read status.
199                          */
200                         return rc;
201
202                 /* Add MFR_SPECIFIC bits to the standard pmbus status regs. */
203                 if (reg == PMBUS_STATUS_FAN_12) {
204                         if (mfr & CFFPS_MFR_FAN_FAULT)
205                                 rc |= PB_FAN_FAN1_FAULT;
206                 } else if (reg == PMBUS_STATUS_TEMPERATURE) {
207                         if (mfr & CFFPS_MFR_THERMAL_FAULT)
208                                 rc |= PB_TEMP_OT_FAULT;
209                 } else if (reg == PMBUS_STATUS_VOUT) {
210                         if (mfr & (CFFPS_MFR_OV_FAULT | CFFPS_MFR_VAUX_FAULT))
211                                 rc |= PB_VOLTAGE_OV_FAULT;
212                         if (mfr & CFFPS_MFR_UV_FAULT)
213                                 rc |= PB_VOLTAGE_UV_FAULT;
214                 } else if (reg == PMBUS_STATUS_IOUT) {
215                         if (mfr & CFFPS_MFR_OC_FAULT)
216                                 rc |= PB_IOUT_OC_FAULT;
217                         if (mfr & CFFPS_MFR_CURRENT_SHARE_WARNING)
218                                 rc |= PB_CURRENT_SHARE_FAULT;
219                 }
220                 break;
221         default:
222                 rc = -ENODATA;
223                 break;
224         }
225
226         return rc;
227 }
228
229 static int ibm_cffps_read_word_data(struct i2c_client *client, int page,
230                                     int reg)
231 {
232         int rc, mfr;
233
234         switch (reg) {
235         case PMBUS_STATUS_WORD:
236                 rc = pmbus_read_word_data(client, page, reg);
237                 if (rc < 0)
238                         return rc;
239
240                 mfr = pmbus_read_byte_data(client, page,
241                                            PMBUS_STATUS_MFR_SPECIFIC);
242                 if (mfr < 0)
243                         /*
244                          * Return the status register instead of an error,
245                          * since we successfully read status.
246                          */
247                         return rc;
248
249                 if (mfr & CFFPS_MFR_PS_KILL)
250                         rc |= PB_STATUS_OFF;
251                 break;
252         default:
253                 rc = -ENODATA;
254                 break;
255         }
256
257         return rc;
258 }
259
260 static struct pmbus_driver_info ibm_cffps_info = {
261         .pages = 1,
262         .func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT |
263                 PMBUS_HAVE_PIN | PMBUS_HAVE_FAN12 | PMBUS_HAVE_TEMP |
264                 PMBUS_HAVE_TEMP2 | PMBUS_HAVE_TEMP3 | PMBUS_HAVE_STATUS_VOUT |
265                 PMBUS_HAVE_STATUS_IOUT | PMBUS_HAVE_STATUS_INPUT |
266                 PMBUS_HAVE_STATUS_TEMP | PMBUS_HAVE_STATUS_FAN12,
267         .read_byte_data = ibm_cffps_read_byte_data,
268         .read_word_data = ibm_cffps_read_word_data,
269 };
270
271 static int ibm_cffps_probe(struct i2c_client *client,
272                            const struct i2c_device_id *id)
273 {
274         int i, rc;
275         struct dentry *debugfs;
276         struct dentry *ibm_cffps_dir;
277         struct ibm_cffps *psu;
278
279         rc = pmbus_do_probe(client, id, &ibm_cffps_info);
280         if (rc)
281                 return rc;
282
283         /* Don't fail the probe if we can't create debugfs */
284         debugfs = pmbus_get_debugfs_dir(client);
285         if (!debugfs)
286                 return 0;
287
288         ibm_cffps_dir = debugfs_create_dir(client->name, debugfs);
289         if (!ibm_cffps_dir)
290                 return 0;
291
292         psu = devm_kzalloc(&client->dev, sizeof(*psu), GFP_KERNEL);
293         if (!psu)
294                 return 0;
295
296         psu->client = client;
297         mutex_init(&psu->input_history.update_lock);
298         psu->input_history.last_update = jiffies - HZ;
299
300         for (i = 0; i < CFFPS_DEBUGFS_NUM_ENTRIES; ++i)
301                 psu->debugfs_entries[i] = i;
302
303         debugfs_create_file("input_history", 0444, ibm_cffps_dir,
304                             &psu->debugfs_entries[CFFPS_DEBUGFS_INPUT_HISTORY],
305                             &ibm_cffps_fops);
306         debugfs_create_file("fru", 0444, ibm_cffps_dir,
307                             &psu->debugfs_entries[CFFPS_DEBUGFS_FRU],
308                             &ibm_cffps_fops);
309         debugfs_create_file("part_number", 0444, ibm_cffps_dir,
310                             &psu->debugfs_entries[CFFPS_DEBUGFS_PN],
311                             &ibm_cffps_fops);
312         debugfs_create_file("serial_number", 0444, ibm_cffps_dir,
313                             &psu->debugfs_entries[CFFPS_DEBUGFS_SN],
314                             &ibm_cffps_fops);
315         debugfs_create_file("ccin", 0444, ibm_cffps_dir,
316                             &psu->debugfs_entries[CFFPS_DEBUGFS_CCIN],
317                             &ibm_cffps_fops);
318         debugfs_create_file("fw_version", 0444, ibm_cffps_dir,
319                             &psu->debugfs_entries[CFFPS_DEBUGFS_FW],
320                             &ibm_cffps_fops);
321
322         return 0;
323 }
324
325 static const struct i2c_device_id ibm_cffps_id[] = {
326         { "ibm_cffps1", 1 },
327         {}
328 };
329 MODULE_DEVICE_TABLE(i2c, ibm_cffps_id);
330
331 static const struct of_device_id ibm_cffps_of_match[] = {
332         { .compatible = "ibm,cffps1" },
333         {}
334 };
335 MODULE_DEVICE_TABLE(of, ibm_cffps_of_match);
336
337 static struct i2c_driver ibm_cffps_driver = {
338         .driver = {
339                 .name = "ibm-cffps",
340                 .of_match_table = ibm_cffps_of_match,
341         },
342         .probe = ibm_cffps_probe,
343         .remove = pmbus_do_remove,
344         .id_table = ibm_cffps_id,
345 };
346
347 module_i2c_driver(ibm_cffps_driver);
348
349 MODULE_AUTHOR("Eddie James");
350 MODULE_DESCRIPTION("PMBus driver for IBM Common Form Factor power supplies");
351 MODULE_LICENSE("GPL");