1 /*
2  * Copyright (c) 2023 Grinn
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #include <zephyr/device.h>
7 
8 #include <zephyr/drivers/mfd/ad559x.h>
9 #include <zephyr/drivers/spi.h>
10 #include <zephyr/sys/byteorder.h>
11 
12 #include "mfd_ad559x.h"
13 
mfd_ad559x_spi_read_raw(const struct device * dev,uint8_t * val,size_t len)14 static int mfd_ad559x_spi_read_raw(const struct device *dev, uint8_t *val, size_t len)
15 {
16 	const struct mfd_ad559x_config *config = dev->config;
17 	uint16_t nop_msg = 0;
18 
19 	struct spi_buf tx_buf[] = {{.buf = &nop_msg, .len = sizeof(nop_msg)}};
20 
21 	const struct spi_buf_set tx = {.buffers = tx_buf, .count = 1};
22 
23 	struct spi_buf rx_buf[] = {{.buf = val, .len = len}};
24 
25 	const struct spi_buf_set rx = {.buffers = rx_buf, .count = 1};
26 
27 	return spi_transceive_dt(&config->spi, &tx, &rx);
28 }
29 
mfd_ad559x_spi_write_raw(const struct device * dev,uint8_t * val,size_t len)30 static int mfd_ad559x_spi_write_raw(const struct device *dev, uint8_t *val, size_t len)
31 {
32 	const struct mfd_ad559x_config *config = dev->config;
33 
34 	struct spi_buf tx_buf[] = {{.buf = val, .len = len}};
35 
36 	const struct spi_buf_set tx = {.buffers = tx_buf, .count = 1};
37 
38 	return spi_write_dt(&config->spi, &tx);
39 }
40 
mfd_ad559x_spi_read_reg(const struct device * dev,uint8_t reg,uint8_t reg_data,uint16_t * val)41 static int mfd_ad559x_spi_read_reg(const struct device *dev, uint8_t reg, uint8_t reg_data,
42 				   uint16_t *val)
43 {
44 	uint16_t data;
45 	uint16_t msg;
46 	int ret;
47 
48 	switch (reg) {
49 	case AD559X_REG_GPIO_INPUT_EN:
50 		msg = sys_cpu_to_be16(AD559X_GPIO_READBACK_EN |
51 				      (AD559X_REG_GPIO_INPUT_EN << AD559X_REG_SHIFT_VAL) |
52 				      reg_data);
53 		break;
54 	default:
55 		msg = sys_cpu_to_be16(AD559X_LDAC_READBACK_EN |
56 				      (AD559X_REG_READ_AND_LDAC << AD559X_REG_SHIFT_VAL) |
57 				      reg << AD559X_REG_READBACK_SHIFT_VAL);
58 		break;
59 	}
60 
61 	ret = mfd_ad559x_spi_write_raw(dev, (uint8_t *)&msg, sizeof(msg));
62 	if (ret < 0) {
63 		return ret;
64 	}
65 
66 	ret = mfd_ad559x_spi_read_raw(dev, (uint8_t *)&data, sizeof(data));
67 	if (ret < 0) {
68 		return ret;
69 	}
70 
71 	*val = sys_be16_to_cpu(data);
72 
73 	return 0;
74 }
75 
mfd_ad559x_spi_write_reg(const struct device * dev,uint8_t reg,uint16_t val)76 static int mfd_ad559x_spi_write_reg(const struct device *dev, uint8_t reg, uint16_t val)
77 {
78 	uint16_t write_mask;
79 	uint16_t msg;
80 
81 	switch (reg) {
82 	case AD559X_REG_SOFTWARE_RESET:
83 		write_mask = AD559X_REG_RESET_VAL_MASK;
84 		break;
85 	default:
86 		write_mask = AD559X_REG_VAL_MASK;
87 		break;
88 	}
89 
90 	msg = sys_cpu_to_be16((reg << AD559X_REG_SHIFT_VAL) | (val & write_mask));
91 
92 	return mfd_ad559x_spi_write_raw(dev, (uint8_t *)&msg, sizeof(msg));
93 }
94 
95 static const struct mfd_ad559x_transfer_function mfd_ad559x_spi_transfer_function = {
96 	.read_raw = mfd_ad559x_spi_read_raw,
97 	.write_raw = mfd_ad559x_spi_write_raw,
98 	.read_reg = mfd_ad559x_spi_read_reg,
99 	.write_reg = mfd_ad559x_spi_write_reg,
100 };
101 
mfd_ad559x_spi_init(const struct device * dev)102 int mfd_ad559x_spi_init(const struct device *dev)
103 {
104 	const struct mfd_ad559x_config *config = dev->config;
105 	struct mfd_ad559x_data *data = dev->data;
106 
107 	data->transfer_function = &mfd_ad559x_spi_transfer_function;
108 
109 	if (!spi_is_ready_dt(&config->spi)) {
110 		return -ENODEV;
111 	}
112 
113 	return 0;
114 }
115