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/module.h>
12 #include <linux/of_device.h>
15 #define STORE_DEFAULT_ALL 0x11
16 #define ERASE_BLACKBOX_DATA 0xD1
17 #define READ_HISTORY_EVENT_NUMBER 0xD2
18 #define READ_HISTORY_EVENTS 0xE0
19 #define SET_HISTORY_EVENT_OFFSET 0xE1
20 #define PMBUS_FLASH_KEY_WRITE 0xEC
27 Q54SJ108A2_DEBUGFS_OPERATION
= 0,
28 Q54SJ108A2_DEBUGFS_CLEARFAULT
,
29 Q54SJ108A2_DEBUGFS_WRITEPROTECT
,
30 Q54SJ108A2_DEBUGFS_STOREDEFAULT
,
31 Q54SJ108A2_DEBUGFS_VOOV_RESPONSE
,
32 Q54SJ108A2_DEBUGFS_IOOC_RESPONSE
,
33 Q54SJ108A2_DEBUGFS_PMBUS_VERSION
,
34 Q54SJ108A2_DEBUGFS_MFR_ID
,
35 Q54SJ108A2_DEBUGFS_MFR_MODEL
,
36 Q54SJ108A2_DEBUGFS_MFR_REVISION
,
37 Q54SJ108A2_DEBUGFS_MFR_LOCATION
,
38 Q54SJ108A2_DEBUGFS_BLACKBOX_ERASE
,
39 Q54SJ108A2_DEBUGFS_BLACKBOX_READ_OFFSET
,
40 Q54SJ108A2_DEBUGFS_BLACKBOX_SET_OFFSET
,
41 Q54SJ108A2_DEBUGFS_BLACKBOX_READ
,
42 Q54SJ108A2_DEBUGFS_FLASH_KEY
,
43 Q54SJ108A2_DEBUGFS_NUM_ENTRIES
46 struct q54sj108a2_data
{
48 struct i2c_client
*client
;
50 int debugfs_entries
[Q54SJ108A2_DEBUGFS_NUM_ENTRIES
];
53 #define to_psu(x, y) container_of((x), struct q54sj108a2_data, debugfs_entries[(y)])
55 static struct pmbus_driver_info q54sj108a2_info
[] = {
59 /* Source : Delta Q54SJ108A2 */
60 .format
[PSC_TEMPERATURE
] = linear
,
61 .format
[PSC_VOLTAGE_IN
] = linear
,
62 .format
[PSC_CURRENT_OUT
] = linear
,
64 .func
[0] = PMBUS_HAVE_VIN
|
65 PMBUS_HAVE_VOUT
| PMBUS_HAVE_STATUS_VOUT
|
66 PMBUS_HAVE_IOUT
| PMBUS_HAVE_STATUS_IOUT
|
67 PMBUS_HAVE_TEMP
| PMBUS_HAVE_STATUS_TEMP
|
68 PMBUS_HAVE_STATUS_INPUT
,
72 static ssize_t
q54sj108a2_debugfs_read(struct file
*file
, char __user
*buf
,
73 size_t count
, loff_t
*ppos
)
76 int *idxp
= file
->private_data
;
78 struct q54sj108a2_data
*psu
= to_psu(idxp
, idx
);
79 char data
[I2C_SMBUS_BLOCK_MAX
+ 2] = { 0 };
80 char data_char
[I2C_SMBUS_BLOCK_MAX
+ 2] = { 0 };
84 case Q54SJ108A2_DEBUGFS_OPERATION
:
85 rc
= i2c_smbus_read_byte_data(psu
->client
, PMBUS_OPERATION
);
89 rc
= snprintf(data
, 3, "%02x", rc
);
91 case Q54SJ108A2_DEBUGFS_WRITEPROTECT
:
92 rc
= i2c_smbus_read_byte_data(psu
->client
, PMBUS_WRITE_PROTECT
);
96 rc
= snprintf(data
, 3, "%02x", rc
);
98 case Q54SJ108A2_DEBUGFS_VOOV_RESPONSE
:
99 rc
= i2c_smbus_read_byte_data(psu
->client
, PMBUS_VOUT_OV_FAULT_RESPONSE
);
103 rc
= snprintf(data
, 3, "%02x", rc
);
105 case Q54SJ108A2_DEBUGFS_IOOC_RESPONSE
:
106 rc
= i2c_smbus_read_byte_data(psu
->client
, PMBUS_IOUT_OC_FAULT_RESPONSE
);
110 rc
= snprintf(data
, 3, "%02x", rc
);
112 case Q54SJ108A2_DEBUGFS_PMBUS_VERSION
:
113 rc
= i2c_smbus_read_byte_data(psu
->client
, PMBUS_REVISION
);
117 rc
= snprintf(data
, 3, "%02x", rc
);
119 case Q54SJ108A2_DEBUGFS_MFR_ID
:
120 rc
= i2c_smbus_read_block_data(psu
->client
, PMBUS_MFR_ID
, data
);
124 case Q54SJ108A2_DEBUGFS_MFR_MODEL
:
125 rc
= i2c_smbus_read_block_data(psu
->client
, PMBUS_MFR_MODEL
, data
);
129 case Q54SJ108A2_DEBUGFS_MFR_REVISION
:
130 rc
= i2c_smbus_read_block_data(psu
->client
, PMBUS_MFR_REVISION
, data
);
134 case Q54SJ108A2_DEBUGFS_MFR_LOCATION
:
135 rc
= i2c_smbus_read_block_data(psu
->client
, PMBUS_MFR_LOCATION
, data
);
139 case Q54SJ108A2_DEBUGFS_BLACKBOX_READ_OFFSET
:
140 rc
= i2c_smbus_read_byte_data(psu
->client
, READ_HISTORY_EVENT_NUMBER
);
144 rc
= snprintf(data
, 3, "%02x", rc
);
146 case Q54SJ108A2_DEBUGFS_BLACKBOX_READ
:
147 rc
= i2c_smbus_read_block_data(psu
->client
, READ_HISTORY_EVENTS
, data
);
151 res
= bin2hex(data
, data_char
, 32);
155 case Q54SJ108A2_DEBUGFS_FLASH_KEY
:
156 rc
= i2c_smbus_read_block_data(psu
->client
, PMBUS_FLASH_KEY_WRITE
, data
);
160 res
= bin2hex(data
, data_char
, 4);
171 return simple_read_from_buffer(buf
, count
, ppos
, data
, rc
);
174 static ssize_t
q54sj108a2_debugfs_write(struct file
*file
, const char __user
*buf
,
175 size_t count
, loff_t
*ppos
)
180 int *idxp
= file
->private_data
;
182 struct q54sj108a2_data
*psu
= to_psu(idxp
, idx
);
184 rc
= i2c_smbus_write_byte_data(psu
->client
, PMBUS_WRITE_PROTECT
, 0);
189 case Q54SJ108A2_DEBUGFS_OPERATION
:
190 rc
= kstrtou8_from_user(buf
, count
, 0, &dst_data
);
194 rc
= i2c_smbus_write_byte_data(psu
->client
, PMBUS_OPERATION
, dst_data
);
199 case Q54SJ108A2_DEBUGFS_CLEARFAULT
:
200 rc
= i2c_smbus_write_byte(psu
->client
, PMBUS_CLEAR_FAULTS
);
205 case Q54SJ108A2_DEBUGFS_STOREDEFAULT
:
210 rc
= i2c_smbus_write_block_data(psu
->client
, PMBUS_FLASH_KEY_WRITE
, 4, flash_key
);
214 rc
= i2c_smbus_write_byte(psu
->client
, STORE_DEFAULT_ALL
);
219 case Q54SJ108A2_DEBUGFS_VOOV_RESPONSE
:
220 rc
= kstrtou8_from_user(buf
, count
, 0, &dst_data
);
224 rc
= i2c_smbus_write_byte_data(psu
->client
, PMBUS_VOUT_OV_FAULT_RESPONSE
, dst_data
);
229 case Q54SJ108A2_DEBUGFS_IOOC_RESPONSE
:
230 rc
= kstrtou8_from_user(buf
, count
, 0, &dst_data
);
234 rc
= i2c_smbus_write_byte_data(psu
->client
, PMBUS_IOUT_OC_FAULT_RESPONSE
, dst_data
);
239 case Q54SJ108A2_DEBUGFS_BLACKBOX_ERASE
:
240 rc
= i2c_smbus_write_byte(psu
->client
, ERASE_BLACKBOX_DATA
);
245 case Q54SJ108A2_DEBUGFS_BLACKBOX_SET_OFFSET
:
246 rc
= kstrtou8_from_user(buf
, count
, 0, &dst_data
);
250 rc
= i2c_smbus_write_byte_data(psu
->client
, SET_HISTORY_EVENT_OFFSET
, dst_data
);
262 static const struct file_operations q54sj108a2_fops
= {
263 .llseek
= noop_llseek
,
264 .read
= q54sj108a2_debugfs_read
,
265 .write
= q54sj108a2_debugfs_write
,
269 static const struct i2c_device_id q54sj108a2_id
[] = {
270 { "q54sj108a2", q54sj108a2
},
274 MODULE_DEVICE_TABLE(i2c
, q54sj108a2_id
);
276 static int q54sj108a2_probe(struct i2c_client
*client
)
278 struct device
*dev
= &client
->dev
;
279 u8 buf
[I2C_SMBUS_BLOCK_MAX
+ 1];
282 struct dentry
*debugfs
;
283 struct dentry
*q54sj108a2_dir
;
284 struct q54sj108a2_data
*psu
;
286 if (!i2c_check_functionality(client
->adapter
,
287 I2C_FUNC_SMBUS_BYTE_DATA
|
288 I2C_FUNC_SMBUS_WORD_DATA
|
289 I2C_FUNC_SMBUS_BLOCK_DATA
))
292 if (client
->dev
.of_node
)
293 chip_id
= (enum chips
)(unsigned long)of_device_get_match_data(dev
);
295 chip_id
= i2c_match_id(q54sj108a2_id
, client
)->driver_data
;
297 ret
= i2c_smbus_read_block_data(client
, PMBUS_MFR_ID
, buf
);
299 dev_err(&client
->dev
, "Failed to read Manufacturer ID\n");
302 if (ret
!= 5 || strncmp(buf
, "DELTA", 5)) {
304 dev_err(dev
, "Unsupported Manufacturer ID '%s'\n", buf
);
309 * The chips support reading PMBUS_MFR_MODEL.
311 ret
= i2c_smbus_read_block_data(client
, PMBUS_MFR_MODEL
, buf
);
313 dev_err(dev
, "Failed to read Manufacturer Model\n");
316 if (ret
!= 14 || strncmp(buf
, "Q54SJ108A2", 10)) {
318 dev_err(dev
, "Unsupported Manufacturer Model '%s'\n", buf
);
322 ret
= i2c_smbus_read_block_data(client
, PMBUS_MFR_REVISION
, buf
);
324 dev_err(dev
, "Failed to read Manufacturer Revision\n");
327 if (ret
!= 4 || buf
[0] != 'S') {
329 dev_err(dev
, "Unsupported Manufacturer Revision '%s'\n", buf
);
333 ret
= pmbus_do_probe(client
, &q54sj108a2_info
[chip_id
]);
337 psu
= devm_kzalloc(&client
->dev
, sizeof(*psu
), GFP_KERNEL
);
341 psu
->client
= client
;
343 debugfs
= pmbus_get_debugfs_dir(client
);
345 q54sj108a2_dir
= debugfs_create_dir(client
->name
, debugfs
);
347 for (i
= 0; i
< Q54SJ108A2_DEBUGFS_NUM_ENTRIES
; ++i
)
348 psu
->debugfs_entries
[i
] = i
;
350 debugfs_create_file("operation", 0644, q54sj108a2_dir
,
351 &psu
->debugfs_entries
[Q54SJ108A2_DEBUGFS_OPERATION
],
353 debugfs_create_file("clear_fault", 0200, q54sj108a2_dir
,
354 &psu
->debugfs_entries
[Q54SJ108A2_DEBUGFS_CLEARFAULT
],
356 debugfs_create_file("write_protect", 0444, q54sj108a2_dir
,
357 &psu
->debugfs_entries
[Q54SJ108A2_DEBUGFS_WRITEPROTECT
],
359 debugfs_create_file("store_default", 0200, q54sj108a2_dir
,
360 &psu
->debugfs_entries
[Q54SJ108A2_DEBUGFS_STOREDEFAULT
],
362 debugfs_create_file("vo_ov_response", 0644, q54sj108a2_dir
,
363 &psu
->debugfs_entries
[Q54SJ108A2_DEBUGFS_VOOV_RESPONSE
],
365 debugfs_create_file("io_oc_response", 0644, q54sj108a2_dir
,
366 &psu
->debugfs_entries
[Q54SJ108A2_DEBUGFS_IOOC_RESPONSE
],
368 debugfs_create_file("pmbus_revision", 0444, q54sj108a2_dir
,
369 &psu
->debugfs_entries
[Q54SJ108A2_DEBUGFS_PMBUS_VERSION
],
371 debugfs_create_file("mfr_id", 0444, q54sj108a2_dir
,
372 &psu
->debugfs_entries
[Q54SJ108A2_DEBUGFS_MFR_ID
],
374 debugfs_create_file("mfr_model", 0444, q54sj108a2_dir
,
375 &psu
->debugfs_entries
[Q54SJ108A2_DEBUGFS_MFR_MODEL
],
377 debugfs_create_file("mfr_revision", 0444, q54sj108a2_dir
,
378 &psu
->debugfs_entries
[Q54SJ108A2_DEBUGFS_MFR_REVISION
],
380 debugfs_create_file("mfr_location", 0444, q54sj108a2_dir
,
381 &psu
->debugfs_entries
[Q54SJ108A2_DEBUGFS_MFR_LOCATION
],
383 debugfs_create_file("blackbox_erase", 0200, q54sj108a2_dir
,
384 &psu
->debugfs_entries
[Q54SJ108A2_DEBUGFS_BLACKBOX_ERASE
],
386 debugfs_create_file("blackbox_read_offset", 0444, q54sj108a2_dir
,
387 &psu
->debugfs_entries
[Q54SJ108A2_DEBUGFS_BLACKBOX_READ_OFFSET
],
389 debugfs_create_file("blackbox_set_offset", 0200, q54sj108a2_dir
,
390 &psu
->debugfs_entries
[Q54SJ108A2_DEBUGFS_BLACKBOX_SET_OFFSET
],
392 debugfs_create_file("blackbox_read", 0444, q54sj108a2_dir
,
393 &psu
->debugfs_entries
[Q54SJ108A2_DEBUGFS_BLACKBOX_READ
],
395 debugfs_create_file("flash_key", 0444, q54sj108a2_dir
,
396 &psu
->debugfs_entries
[Q54SJ108A2_DEBUGFS_FLASH_KEY
],
402 static const struct of_device_id q54sj108a2_of_match
[] = {
403 { .compatible
= "delta,q54sj108a2", .data
= (void *)q54sj108a2
},
407 MODULE_DEVICE_TABLE(of
, q54sj108a2_of_match
);
409 static struct i2c_driver q54sj108a2_driver
= {
411 .name
= "q54sj108a2",
412 .of_match_table
= q54sj108a2_of_match
,
414 .probe_new
= q54sj108a2_probe
,
415 .id_table
= q54sj108a2_id
,
418 module_i2c_driver(q54sj108a2_driver
);
420 MODULE_AUTHOR("Xiao.Ma <xiao.mx.ma@deltaww.com>");
421 MODULE_DESCRIPTION("PMBus driver for Delta Q54SJ108A2 series modules");
422 MODULE_LICENSE("GPL");