1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for Delta modules, Q54SJ108A2 series 1/4 Brick DC/DC
4 * Regulated Power Module
6 * Copyright 2020 Delta LLC.
9 #include <linux/debugfs.h>
10 #include <linux/i2c.h>
11 #include <linux/kstrtox.h>
12 #include <linux/module.h>
16 #define STORE_DEFAULT_ALL 0x11
17 #define ERASE_BLACKBOX_DATA 0xD1
18 #define READ_HISTORY_EVENT_NUMBER 0xD2
19 #define READ_HISTORY_EVENTS 0xE0
20 #define SET_HISTORY_EVENT_OFFSET 0xE1
21 #define PMBUS_FLASH_KEY_WRITE 0xEC
28 Q54SJ108A2_DEBUGFS_OPERATION
= 0,
29 Q54SJ108A2_DEBUGFS_CLEARFAULT
,
30 Q54SJ108A2_DEBUGFS_WRITEPROTECT
,
31 Q54SJ108A2_DEBUGFS_STOREDEFAULT
,
32 Q54SJ108A2_DEBUGFS_VOOV_RESPONSE
,
33 Q54SJ108A2_DEBUGFS_IOOC_RESPONSE
,
34 Q54SJ108A2_DEBUGFS_PMBUS_VERSION
,
35 Q54SJ108A2_DEBUGFS_MFR_ID
,
36 Q54SJ108A2_DEBUGFS_MFR_MODEL
,
37 Q54SJ108A2_DEBUGFS_MFR_REVISION
,
38 Q54SJ108A2_DEBUGFS_MFR_LOCATION
,
39 Q54SJ108A2_DEBUGFS_BLACKBOX_ERASE
,
40 Q54SJ108A2_DEBUGFS_BLACKBOX_READ_OFFSET
,
41 Q54SJ108A2_DEBUGFS_BLACKBOX_SET_OFFSET
,
42 Q54SJ108A2_DEBUGFS_BLACKBOX_READ
,
43 Q54SJ108A2_DEBUGFS_FLASH_KEY
,
44 Q54SJ108A2_DEBUGFS_NUM_ENTRIES
47 struct q54sj108a2_data
{
49 struct i2c_client
*client
;
51 int debugfs_entries
[Q54SJ108A2_DEBUGFS_NUM_ENTRIES
];
54 #define to_psu(x, y) container_of((x), struct q54sj108a2_data, debugfs_entries[(y)])
56 static struct pmbus_driver_info q54sj108a2_info
[] = {
60 /* Source : Delta Q54SJ108A2 */
61 .format
[PSC_TEMPERATURE
] = linear
,
62 .format
[PSC_VOLTAGE_IN
] = linear
,
63 .format
[PSC_CURRENT_OUT
] = linear
,
65 .func
[0] = PMBUS_HAVE_VIN
|
66 PMBUS_HAVE_VOUT
| PMBUS_HAVE_STATUS_VOUT
|
67 PMBUS_HAVE_IOUT
| PMBUS_HAVE_STATUS_IOUT
|
68 PMBUS_HAVE_TEMP
| PMBUS_HAVE_STATUS_TEMP
|
69 PMBUS_HAVE_STATUS_INPUT
,
73 static ssize_t
q54sj108a2_debugfs_read(struct file
*file
, char __user
*buf
,
74 size_t count
, loff_t
*ppos
)
77 int *idxp
= file
->private_data
;
79 struct q54sj108a2_data
*psu
= to_psu(idxp
, idx
);
80 char data
[I2C_SMBUS_BLOCK_MAX
+ 2] = { 0 };
81 char data_char
[I2C_SMBUS_BLOCK_MAX
+ 2] = { 0 };
85 case Q54SJ108A2_DEBUGFS_OPERATION
:
86 rc
= i2c_smbus_read_byte_data(psu
->client
, PMBUS_OPERATION
);
90 rc
= snprintf(data
, 3, "%02x", rc
);
92 case Q54SJ108A2_DEBUGFS_WRITEPROTECT
:
93 rc
= i2c_smbus_read_byte_data(psu
->client
, PMBUS_WRITE_PROTECT
);
97 rc
= snprintf(data
, 3, "%02x", rc
);
99 case Q54SJ108A2_DEBUGFS_VOOV_RESPONSE
:
100 rc
= i2c_smbus_read_byte_data(psu
->client
, PMBUS_VOUT_OV_FAULT_RESPONSE
);
104 rc
= snprintf(data
, 3, "%02x", rc
);
106 case Q54SJ108A2_DEBUGFS_IOOC_RESPONSE
:
107 rc
= i2c_smbus_read_byte_data(psu
->client
, PMBUS_IOUT_OC_FAULT_RESPONSE
);
111 rc
= snprintf(data
, 3, "%02x", rc
);
113 case Q54SJ108A2_DEBUGFS_PMBUS_VERSION
:
114 rc
= i2c_smbus_read_byte_data(psu
->client
, PMBUS_REVISION
);
118 rc
= snprintf(data
, 3, "%02x", rc
);
120 case Q54SJ108A2_DEBUGFS_MFR_ID
:
121 rc
= i2c_smbus_read_block_data(psu
->client
, PMBUS_MFR_ID
, data
);
125 case Q54SJ108A2_DEBUGFS_MFR_MODEL
:
126 rc
= i2c_smbus_read_block_data(psu
->client
, PMBUS_MFR_MODEL
, data
);
130 case Q54SJ108A2_DEBUGFS_MFR_REVISION
:
131 rc
= i2c_smbus_read_block_data(psu
->client
, PMBUS_MFR_REVISION
, data
);
135 case Q54SJ108A2_DEBUGFS_MFR_LOCATION
:
136 rc
= i2c_smbus_read_block_data(psu
->client
, PMBUS_MFR_LOCATION
, data
);
140 case Q54SJ108A2_DEBUGFS_BLACKBOX_READ_OFFSET
:
141 rc
= i2c_smbus_read_byte_data(psu
->client
, READ_HISTORY_EVENT_NUMBER
);
145 rc
= snprintf(data
, 3, "%02x", rc
);
147 case Q54SJ108A2_DEBUGFS_BLACKBOX_READ
:
148 rc
= i2c_smbus_read_block_data(psu
->client
, READ_HISTORY_EVENTS
, data
);
152 res
= bin2hex(data
, data_char
, 32);
156 case Q54SJ108A2_DEBUGFS_FLASH_KEY
:
157 rc
= i2c_smbus_read_block_data(psu
->client
, PMBUS_FLASH_KEY_WRITE
, data
);
161 res
= bin2hex(data
, data_char
, 4);
172 return simple_read_from_buffer(buf
, count
, ppos
, data
, rc
);
175 static ssize_t
q54sj108a2_debugfs_write(struct file
*file
, const char __user
*buf
,
176 size_t count
, loff_t
*ppos
)
181 int *idxp
= file
->private_data
;
183 struct q54sj108a2_data
*psu
= to_psu(idxp
, idx
);
185 rc
= i2c_smbus_write_byte_data(psu
->client
, PMBUS_WRITE_PROTECT
, 0);
190 case Q54SJ108A2_DEBUGFS_OPERATION
:
191 rc
= kstrtou8_from_user(buf
, count
, 0, &dst_data
);
195 rc
= i2c_smbus_write_byte_data(psu
->client
, PMBUS_OPERATION
, dst_data
);
200 case Q54SJ108A2_DEBUGFS_CLEARFAULT
:
201 rc
= i2c_smbus_write_byte(psu
->client
, PMBUS_CLEAR_FAULTS
);
206 case Q54SJ108A2_DEBUGFS_STOREDEFAULT
:
211 rc
= i2c_smbus_write_block_data(psu
->client
, PMBUS_FLASH_KEY_WRITE
, 4, flash_key
);
215 rc
= i2c_smbus_write_byte(psu
->client
, STORE_DEFAULT_ALL
);
220 case Q54SJ108A2_DEBUGFS_VOOV_RESPONSE
:
221 rc
= kstrtou8_from_user(buf
, count
, 0, &dst_data
);
225 rc
= i2c_smbus_write_byte_data(psu
->client
, PMBUS_VOUT_OV_FAULT_RESPONSE
, dst_data
);
230 case Q54SJ108A2_DEBUGFS_IOOC_RESPONSE
:
231 rc
= kstrtou8_from_user(buf
, count
, 0, &dst_data
);
235 rc
= i2c_smbus_write_byte_data(psu
->client
, PMBUS_IOUT_OC_FAULT_RESPONSE
, dst_data
);
240 case Q54SJ108A2_DEBUGFS_BLACKBOX_ERASE
:
241 rc
= i2c_smbus_write_byte(psu
->client
, ERASE_BLACKBOX_DATA
);
246 case Q54SJ108A2_DEBUGFS_BLACKBOX_SET_OFFSET
:
247 rc
= kstrtou8_from_user(buf
, count
, 0, &dst_data
);
251 rc
= i2c_smbus_write_byte_data(psu
->client
, SET_HISTORY_EVENT_OFFSET
, dst_data
);
263 static const struct file_operations q54sj108a2_fops
= {
264 .llseek
= noop_llseek
,
265 .read
= q54sj108a2_debugfs_read
,
266 .write
= q54sj108a2_debugfs_write
,
270 static const struct i2c_device_id q54sj108a2_id
[] = {
271 { "q54sj108a2", q54sj108a2
},
275 MODULE_DEVICE_TABLE(i2c
, q54sj108a2_id
);
277 static int q54sj108a2_probe(struct i2c_client
*client
)
279 struct device
*dev
= &client
->dev
;
280 u8 buf
[I2C_SMBUS_BLOCK_MAX
+ 1];
283 struct dentry
*debugfs
;
284 struct dentry
*q54sj108a2_dir
;
285 struct q54sj108a2_data
*psu
;
287 if (!i2c_check_functionality(client
->adapter
,
288 I2C_FUNC_SMBUS_BYTE_DATA
|
289 I2C_FUNC_SMBUS_WORD_DATA
|
290 I2C_FUNC_SMBUS_BLOCK_DATA
))
293 if (client
->dev
.of_node
)
294 chip_id
= (enum chips
)(unsigned long)of_device_get_match_data(dev
);
296 chip_id
= i2c_match_id(q54sj108a2_id
, client
)->driver_data
;
298 ret
= i2c_smbus_read_block_data(client
, PMBUS_MFR_ID
, buf
);
300 dev_err(&client
->dev
, "Failed to read Manufacturer ID\n");
303 if (ret
!= 6 || strncmp(buf
, "DELTA", 5)) {
305 dev_err(dev
, "Unsupported Manufacturer ID '%s'\n", buf
);
310 * The chips support reading PMBUS_MFR_MODEL.
312 ret
= i2c_smbus_read_block_data(client
, PMBUS_MFR_MODEL
, buf
);
314 dev_err(dev
, "Failed to read Manufacturer Model\n");
317 if (ret
!= 14 || strncmp(buf
, "Q54SJ108A2", 10)) {
319 dev_err(dev
, "Unsupported Manufacturer Model '%s'\n", buf
);
323 ret
= i2c_smbus_read_block_data(client
, PMBUS_MFR_REVISION
, buf
);
325 dev_err(dev
, "Failed to read Manufacturer Revision\n");
328 if (ret
!= 4 || buf
[0] != 'S') {
330 dev_err(dev
, "Unsupported Manufacturer Revision '%s'\n", buf
);
334 ret
= pmbus_do_probe(client
, &q54sj108a2_info
[chip_id
]);
338 psu
= devm_kzalloc(&client
->dev
, sizeof(*psu
), GFP_KERNEL
);
342 psu
->client
= client
;
344 debugfs
= pmbus_get_debugfs_dir(client
);
346 q54sj108a2_dir
= debugfs_create_dir(client
->name
, debugfs
);
348 for (i
= 0; i
< Q54SJ108A2_DEBUGFS_NUM_ENTRIES
; ++i
)
349 psu
->debugfs_entries
[i
] = i
;
351 debugfs_create_file("operation", 0644, q54sj108a2_dir
,
352 &psu
->debugfs_entries
[Q54SJ108A2_DEBUGFS_OPERATION
],
354 debugfs_create_file("clear_fault", 0200, q54sj108a2_dir
,
355 &psu
->debugfs_entries
[Q54SJ108A2_DEBUGFS_CLEARFAULT
],
357 debugfs_create_file("write_protect", 0444, q54sj108a2_dir
,
358 &psu
->debugfs_entries
[Q54SJ108A2_DEBUGFS_WRITEPROTECT
],
360 debugfs_create_file("store_default", 0200, q54sj108a2_dir
,
361 &psu
->debugfs_entries
[Q54SJ108A2_DEBUGFS_STOREDEFAULT
],
363 debugfs_create_file("vo_ov_response", 0644, q54sj108a2_dir
,
364 &psu
->debugfs_entries
[Q54SJ108A2_DEBUGFS_VOOV_RESPONSE
],
366 debugfs_create_file("io_oc_response", 0644, q54sj108a2_dir
,
367 &psu
->debugfs_entries
[Q54SJ108A2_DEBUGFS_IOOC_RESPONSE
],
369 debugfs_create_file("pmbus_revision", 0444, q54sj108a2_dir
,
370 &psu
->debugfs_entries
[Q54SJ108A2_DEBUGFS_PMBUS_VERSION
],
372 debugfs_create_file("mfr_id", 0444, q54sj108a2_dir
,
373 &psu
->debugfs_entries
[Q54SJ108A2_DEBUGFS_MFR_ID
],
375 debugfs_create_file("mfr_model", 0444, q54sj108a2_dir
,
376 &psu
->debugfs_entries
[Q54SJ108A2_DEBUGFS_MFR_MODEL
],
378 debugfs_create_file("mfr_revision", 0444, q54sj108a2_dir
,
379 &psu
->debugfs_entries
[Q54SJ108A2_DEBUGFS_MFR_REVISION
],
381 debugfs_create_file("mfr_location", 0444, q54sj108a2_dir
,
382 &psu
->debugfs_entries
[Q54SJ108A2_DEBUGFS_MFR_LOCATION
],
384 debugfs_create_file("blackbox_erase", 0200, q54sj108a2_dir
,
385 &psu
->debugfs_entries
[Q54SJ108A2_DEBUGFS_BLACKBOX_ERASE
],
387 debugfs_create_file("blackbox_read_offset", 0444, q54sj108a2_dir
,
388 &psu
->debugfs_entries
[Q54SJ108A2_DEBUGFS_BLACKBOX_READ_OFFSET
],
390 debugfs_create_file("blackbox_set_offset", 0200, q54sj108a2_dir
,
391 &psu
->debugfs_entries
[Q54SJ108A2_DEBUGFS_BLACKBOX_SET_OFFSET
],
393 debugfs_create_file("blackbox_read", 0444, q54sj108a2_dir
,
394 &psu
->debugfs_entries
[Q54SJ108A2_DEBUGFS_BLACKBOX_READ
],
396 debugfs_create_file("flash_key", 0444, q54sj108a2_dir
,
397 &psu
->debugfs_entries
[Q54SJ108A2_DEBUGFS_FLASH_KEY
],
403 static const struct of_device_id q54sj108a2_of_match
[] = {
404 { .compatible
= "delta,q54sj108a2", .data
= (void *)q54sj108a2
},
408 MODULE_DEVICE_TABLE(of
, q54sj108a2_of_match
);
410 static struct i2c_driver q54sj108a2_driver
= {
412 .name
= "q54sj108a2",
413 .of_match_table
= q54sj108a2_of_match
,
415 .probe
= q54sj108a2_probe
,
416 .id_table
= q54sj108a2_id
,
419 module_i2c_driver(q54sj108a2_driver
);
421 MODULE_AUTHOR("Xiao.Ma <xiao.mx.ma@deltaww.com>");
422 MODULE_DESCRIPTION("PMBus driver for Delta Q54SJ108A2 series modules");
423 MODULE_LICENSE("GPL");
424 MODULE_IMPORT_NS(PMBUS
);