1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <device/i2c_bus.h>
4 #include <device/device.h>
5 #include "pca9538.h"
6 #include "chip.h"
7
8 /* This function can be used from outside the chip driver to read input. */
pca9538_read_input(void)9 uint8_t pca9538_read_input(void)
10 {
11 struct device *dev = pca9538_get_dev();
12
13 if (!dev)
14 return 0;
15 else
16 return (uint8_t)(i2c_dev_readb_at(dev, INPUT_REG));
17 }
18
19 /* This function can be used from outside the chip driver to set output. */
pca9538_set_output(uint8_t val)20 void pca9538_set_output(uint8_t val)
21 {
22 struct device *dev = pca9538_get_dev();
23
24 if (dev)
25 i2c_dev_writeb_at(dev, OUTPUT_REG, val);
26 }
27
pca9538_init(struct device * dev)28 static void pca9538_init(struct device *dev)
29 {
30 struct drivers_i2c_pca9538_config *config = dev->chip_info;
31
32 if (!config)
33 return;
34 /* Set up registers as requested in devicetree. */
35 i2c_dev_writeb_at(dev, INPUT_INVERT_REG, config->invert);
36 i2c_dev_writeb_at(dev, OUTPUT_REG, config->out_val);
37 i2c_dev_writeb_at(dev, IO_CONFIG_REG, config->in_out);
38 }
39
40 static struct device_operations pca9538_ops = {
41 .read_resources = noop_read_resources,
42 .set_resources = noop_set_resources,
43 .init = pca9538_init,
44 };
45
pca9538_enable(struct device * dev)46 static void pca9538_enable(struct device *dev)
47 {
48 dev->ops = &pca9538_ops;
49 }
50
51 struct chip_operations drivers_i2c_pca9538_ops = {
52 .name = "PCA9538",
53 .enable_dev = pca9538_enable
54 };
55