1 // SPDX-License-Identifier: GPL-2.0
3 * ADM1266 - Cascadable Super Sequencer with Margin
4 * Control and Fault Recording
6 * Copyright 2020 Analog Devices Inc.
9 #include <linux/bitfield.h>
10 #include <linux/crc8.h>
11 #include <linux/debugfs.h>
12 #include <linux/gpio/driver.h>
13 #include <linux/i2c.h>
14 #include <linux/i2c-smbus.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/nvmem-consumer.h>
19 #include <linux/nvmem-provider.h>
21 #include <linux/slab.h>
22 #include <linux/timekeeping.h>
24 #define ADM1266_BLACKBOX_CONFIG 0xD3
25 #define ADM1266_PDIO_CONFIG 0xD4
26 #define ADM1266_READ_STATE 0xD9
27 #define ADM1266_READ_BLACKBOX 0xDE
28 #define ADM1266_SET_RTC 0xDF
29 #define ADM1266_GPIO_CONFIG 0xE1
30 #define ADM1266_BLACKBOX_INFO 0xE6
31 #define ADM1266_PDIO_STATUS 0xE9
32 #define ADM1266_GPIO_STATUS 0xEA
34 /* ADM1266 GPIO defines */
35 #define ADM1266_GPIO_NR 9
36 #define ADM1266_GPIO_FUNCTIONS(x) FIELD_GET(BIT(0), x)
37 #define ADM1266_GPIO_INPUT_EN(x) FIELD_GET(BIT(2), x)
38 #define ADM1266_GPIO_OUTPUT_EN(x) FIELD_GET(BIT(3), x)
39 #define ADM1266_GPIO_OPEN_DRAIN(x) FIELD_GET(BIT(4), x)
41 /* ADM1266 PDIO defines */
42 #define ADM1266_PDIO_NR 16
43 #define ADM1266_PDIO_PIN_CFG(x) FIELD_GET(GENMASK(15, 13), x)
44 #define ADM1266_PDIO_GLITCH_FILT(x) FIELD_GET(GENMASK(12, 9), x)
45 #define ADM1266_PDIO_OUT_CFG(x) FIELD_GET(GENMASK(2, 0), x)
47 #define ADM1266_BLACKBOX_OFFSET 0
48 #define ADM1266_BLACKBOX_SIZE 64
50 #define ADM1266_PMBUS_BLOCK_MAX 255
53 struct pmbus_driver_info info
;
55 const char *gpio_names
[ADM1266_GPIO_NR
+ ADM1266_PDIO_NR
];
56 struct i2c_client
*client
;
57 struct dentry
*debugfs_dir
;
58 struct nvmem_config nvmem_config
;
59 struct nvmem_device
*nvmem
;
61 struct mutex buf_mutex
;
62 u8 write_buf
[ADM1266_PMBUS_BLOCK_MAX
+ 1] ____cacheline_aligned
;
63 u8 read_buf
[ADM1266_PMBUS_BLOCK_MAX
+ 1] ____cacheline_aligned
;
66 static const struct nvmem_cell_info adm1266_nvmem_cells
[] = {
69 .offset
= ADM1266_BLACKBOX_OFFSET
,
74 DECLARE_CRC8_TABLE(pmbus_crc_table
);
77 * Different from Block Read as it sends data and waits for the slave to
78 * return a value dependent on that data. The protocol is simply a Write Block
79 * followed by a Read Block without the Read-Block command field and the
80 * Write-Block STOP bit.
82 static int adm1266_pmbus_block_xfer(struct adm1266_data
*data
, u8 cmd
, u8 w_len
, u8
*data_w
,
85 struct i2c_client
*client
= data
->client
;
86 struct i2c_msg msgs
[2] = {
89 .flags
= I2C_M_DMA_SAFE
,
90 .buf
= data
->write_buf
,
95 .flags
= I2C_M_RD
| I2C_M_DMA_SAFE
,
96 .buf
= data
->read_buf
,
97 .len
= ADM1266_PMBUS_BLOCK_MAX
+ 2,
104 mutex_lock(&data
->buf_mutex
);
106 msgs
[0].buf
[0] = cmd
;
107 msgs
[0].buf
[1] = w_len
;
108 memcpy(&msgs
[0].buf
[2], data_w
, w_len
);
110 ret
= i2c_transfer(client
->adapter
, msgs
, 2);
115 mutex_unlock(&data
->buf_mutex
);
120 if (client
->flags
& I2C_CLIENT_PEC
) {
121 addr
= i2c_8bit_addr_from_msg(&msgs
[0]);
122 crc
= crc8(pmbus_crc_table
, &addr
, 1, 0);
123 crc
= crc8(pmbus_crc_table
, msgs
[0].buf
, msgs
[0].len
, crc
);
125 addr
= i2c_8bit_addr_from_msg(&msgs
[1]);
126 crc
= crc8(pmbus_crc_table
, &addr
, 1, crc
);
127 crc
= crc8(pmbus_crc_table
, msgs
[1].buf
, msgs
[1].buf
[0] + 1, crc
);
129 if (crc
!= msgs
[1].buf
[msgs
[1].buf
[0] + 1]) {
130 mutex_unlock(&data
->buf_mutex
);
135 memcpy(data_r
, &msgs
[1].buf
[1], msgs
[1].buf
[0]);
137 ret
= msgs
[1].buf
[0];
138 mutex_unlock(&data
->buf_mutex
);
143 static const unsigned int adm1266_gpio_mapping
[ADM1266_GPIO_NR
][2] = {
155 static const char *adm1266_names
[ADM1266_GPIO_NR
+ ADM1266_PDIO_NR
] = {
156 "GPIO1", "GPIO2", "GPIO3", "GPIO4", "GPIO5", "GPIO6", "GPIO7", "GPIO8",
157 "GPIO9", "PDIO1", "PDIO2", "PDIO3", "PDIO4", "PDIO5", "PDIO6",
158 "PDIO7", "PDIO8", "PDIO9", "PDIO10", "PDIO11", "PDIO12", "PDIO13",
159 "PDIO14", "PDIO15", "PDIO16",
162 static int adm1266_gpio_get(struct gpio_chip
*chip
, unsigned int offset
)
164 struct adm1266_data
*data
= gpiochip_get_data(chip
);
165 u8 read_buf
[I2C_SMBUS_BLOCK_MAX
+ 1];
166 unsigned long pins_status
;
167 unsigned int pmbus_cmd
;
170 if (offset
< ADM1266_GPIO_NR
)
171 pmbus_cmd
= ADM1266_GPIO_STATUS
;
173 pmbus_cmd
= ADM1266_PDIO_STATUS
;
175 ret
= i2c_smbus_read_block_data(data
->client
, pmbus_cmd
, read_buf
);
179 pins_status
= read_buf
[0] + (read_buf
[1] << 8);
180 if (offset
< ADM1266_GPIO_NR
)
181 return test_bit(adm1266_gpio_mapping
[offset
][1], &pins_status
);
183 return test_bit(offset
- ADM1266_GPIO_NR
, &pins_status
);
186 static int adm1266_gpio_get_multiple(struct gpio_chip
*chip
, unsigned long *mask
,
189 struct adm1266_data
*data
= gpiochip_get_data(chip
);
190 u8 read_buf
[ADM1266_PMBUS_BLOCK_MAX
+ 1];
191 unsigned long status
;
192 unsigned int gpio_nr
;
195 ret
= i2c_smbus_read_block_data(data
->client
, ADM1266_GPIO_STATUS
, read_buf
);
199 status
= read_buf
[0] + (read_buf
[1] << 8);
202 for_each_set_bit(gpio_nr
, mask
, ADM1266_GPIO_NR
) {
203 if (test_bit(adm1266_gpio_mapping
[gpio_nr
][1], &status
))
204 set_bit(gpio_nr
, bits
);
207 ret
= i2c_smbus_read_block_data(data
->client
, ADM1266_PDIO_STATUS
, read_buf
);
211 status
= read_buf
[0] + (read_buf
[1] << 8);
214 for_each_set_bit_from(gpio_nr
, mask
, ADM1266_GPIO_NR
+ ADM1266_PDIO_STATUS
) {
215 if (test_bit(gpio_nr
- ADM1266_GPIO_NR
, &status
))
216 set_bit(gpio_nr
, bits
);
222 static void adm1266_gpio_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
224 struct adm1266_data
*data
= gpiochip_get_data(chip
);
225 u8 read_buf
[ADM1266_PMBUS_BLOCK_MAX
+ 1];
226 unsigned long gpio_config
;
227 unsigned long pdio_config
;
228 unsigned long pin_cfg
;
233 for (i
= 0; i
< ADM1266_GPIO_NR
; i
++) {
234 write_cmd
= adm1266_gpio_mapping
[i
][1];
235 ret
= adm1266_pmbus_block_xfer(data
, ADM1266_GPIO_CONFIG
, 1, &write_cmd
, read_buf
);
239 gpio_config
= read_buf
[0];
240 seq_puts(s
, adm1266_names
[i
]);
243 if (!ADM1266_GPIO_FUNCTIONS(gpio_config
)) {
244 seq_puts(s
, "high-Z )\n");
247 if (ADM1266_GPIO_INPUT_EN(gpio_config
))
248 seq_puts(s
, "input ");
249 if (ADM1266_GPIO_OUTPUT_EN(gpio_config
))
250 seq_puts(s
, "output ");
251 if (ADM1266_GPIO_OPEN_DRAIN(gpio_config
))
252 seq_puts(s
, "open-drain )\n");
254 seq_puts(s
, "push-pull )\n");
258 ret
= adm1266_pmbus_block_xfer(data
, ADM1266_PDIO_CONFIG
, 1, &write_cmd
, read_buf
);
262 for (i
= 0; i
< ADM1266_PDIO_NR
; i
++) {
263 seq_puts(s
, adm1266_names
[ADM1266_GPIO_NR
+ i
]);
265 pdio_config
= read_buf
[2 * i
];
266 pdio_config
+= (read_buf
[2 * i
+ 1] << 8);
267 pin_cfg
= ADM1266_PDIO_PIN_CFG(pdio_config
);
270 if (!pin_cfg
|| pin_cfg
> 5) {
271 seq_puts(s
, "high-Z )\n");
275 if (pin_cfg
& BIT(0))
276 seq_puts(s
, "output ");
278 if (pin_cfg
& BIT(1))
279 seq_puts(s
, "input ");
285 static int adm1266_config_gpio(struct adm1266_data
*data
)
287 const char *name
= dev_name(&data
->client
->dev
);
292 for (i
= 0; i
< ARRAY_SIZE(data
->gpio_names
); i
++) {
293 gpio_name
= devm_kasprintf(&data
->client
->dev
, GFP_KERNEL
, "adm1266-%x-%s",
294 data
->client
->addr
, adm1266_names
[i
]);
298 data
->gpio_names
[i
] = gpio_name
;
301 data
->gc
.label
= name
;
302 data
->gc
.parent
= &data
->client
->dev
;
303 data
->gc
.owner
= THIS_MODULE
;
305 data
->gc
.names
= data
->gpio_names
;
306 data
->gc
.ngpio
= ARRAY_SIZE(data
->gpio_names
);
307 data
->gc
.get
= adm1266_gpio_get
;
308 data
->gc
.get_multiple
= adm1266_gpio_get_multiple
;
309 data
->gc
.dbg_show
= adm1266_gpio_dbg_show
;
311 ret
= devm_gpiochip_add_data(&data
->client
->dev
, &data
->gc
, data
);
313 dev_err(&data
->client
->dev
, "GPIO registering failed (%d)\n", ret
);
318 static int adm1266_state_read(struct seq_file
*s
, void *pdata
)
320 struct device
*dev
= s
->private;
321 struct i2c_client
*client
= to_i2c_client(dev
);
324 ret
= i2c_smbus_read_word_data(client
, ADM1266_READ_STATE
);
328 seq_printf(s
, "%d\n", ret
);
333 static void adm1266_init_debugfs(struct adm1266_data
*data
)
337 root
= pmbus_get_debugfs_dir(data
->client
);
341 data
->debugfs_dir
= debugfs_create_dir(data
->client
->name
, root
);
342 if (!data
->debugfs_dir
)
345 debugfs_create_devm_seqfile(&data
->client
->dev
, "sequencer_state", data
->debugfs_dir
,
349 static int adm1266_nvmem_read_blackbox(struct adm1266_data
*data
, u8
*read_buff
)
356 ret
= i2c_smbus_read_block_data(data
->client
, ADM1266_BLACKBOX_INFO
, buf
);
363 record_count
= buf
[3];
365 for (index
= 0; index
< record_count
; index
++) {
366 ret
= adm1266_pmbus_block_xfer(data
, ADM1266_READ_BLACKBOX
, 1, &index
, read_buff
);
370 if (ret
!= ADM1266_BLACKBOX_SIZE
)
373 read_buff
+= ADM1266_BLACKBOX_SIZE
;
379 static int adm1266_nvmem_read(void *priv
, unsigned int offset
, void *val
, size_t bytes
)
381 struct adm1266_data
*data
= priv
;
384 if (offset
+ bytes
> data
->nvmem_config
.size
)
388 memset(data
->dev_mem
, 0, data
->nvmem_config
.size
);
390 ret
= adm1266_nvmem_read_blackbox(data
, data
->dev_mem
);
392 dev_err(&data
->client
->dev
, "Could not read blackbox!");
397 memcpy(val
, data
->dev_mem
+ offset
, bytes
);
402 static int adm1266_config_nvmem(struct adm1266_data
*data
)
404 data
->nvmem_config
.name
= dev_name(&data
->client
->dev
);
405 data
->nvmem_config
.dev
= &data
->client
->dev
;
406 data
->nvmem_config
.root_only
= true;
407 data
->nvmem_config
.read_only
= true;
408 data
->nvmem_config
.owner
= THIS_MODULE
;
409 data
->nvmem_config
.reg_read
= adm1266_nvmem_read
;
410 data
->nvmem_config
.cells
= adm1266_nvmem_cells
;
411 data
->nvmem_config
.ncells
= ARRAY_SIZE(adm1266_nvmem_cells
);
412 data
->nvmem_config
.priv
= data
;
413 data
->nvmem_config
.stride
= 1;
414 data
->nvmem_config
.word_size
= 1;
415 data
->nvmem_config
.size
= adm1266_nvmem_cells
[0].bytes
;
417 data
->dev_mem
= devm_kzalloc(&data
->client
->dev
, data
->nvmem_config
.size
, GFP_KERNEL
);
421 data
->nvmem
= devm_nvmem_register(&data
->client
->dev
, &data
->nvmem_config
);
422 if (IS_ERR(data
->nvmem
)) {
423 dev_err(&data
->client
->dev
, "Could not register nvmem!");
424 return PTR_ERR(data
->nvmem
);
430 static int adm1266_set_rtc(struct adm1266_data
*data
)
436 kt
= ktime_get_seconds();
438 memset(write_buf
, 0, sizeof(write_buf
));
440 for (i
= 0; i
< 4; i
++)
441 write_buf
[2 + i
] = (kt
>> (i
* 8)) & 0xFF;
443 return i2c_smbus_write_block_data(data
->client
, ADM1266_SET_RTC
, sizeof(write_buf
),
447 static int adm1266_probe(struct i2c_client
*client
)
449 struct adm1266_data
*data
;
453 data
= devm_kzalloc(&client
->dev
, sizeof(struct adm1266_data
), GFP_KERNEL
);
457 data
->client
= client
;
458 data
->info
.pages
= 17;
459 data
->info
.format
[PSC_VOLTAGE_OUT
] = linear
;
460 for (i
= 0; i
< data
->info
.pages
; i
++)
461 data
->info
.func
[i
] = PMBUS_HAVE_VOUT
| PMBUS_HAVE_STATUS_VOUT
;
463 crc8_populate_msb(pmbus_crc_table
, 0x7);
464 mutex_init(&data
->buf_mutex
);
466 ret
= adm1266_config_gpio(data
);
470 ret
= adm1266_set_rtc(data
);
474 ret
= adm1266_config_nvmem(data
);
478 ret
= pmbus_do_probe(client
, &data
->info
);
482 adm1266_init_debugfs(data
);
487 static const struct of_device_id adm1266_of_match
[] = {
488 { .compatible
= "adi,adm1266" },
491 MODULE_DEVICE_TABLE(of
, adm1266_of_match
);
493 static const struct i2c_device_id adm1266_id
[] = {
497 MODULE_DEVICE_TABLE(i2c
, adm1266_id
);
499 static struct i2c_driver adm1266_driver
= {
502 .of_match_table
= adm1266_of_match
,
504 .probe_new
= adm1266_probe
,
505 .id_table
= adm1266_id
,
508 module_i2c_driver(adm1266_driver
);
510 MODULE_AUTHOR("Alexandru Tachici <alexandru.tachici@analog.com>");
511 MODULE_DESCRIPTION("PMBus driver for Analog Devices ADM1266");
512 MODULE_LICENSE("GPL v2");