1 /* sensor_sx9500.c - Driver for Semtech SX9500 SAR proximity chip */
2
3 /*
4 * Copyright (c) 2016 Intel Corporation
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9 #define DT_DRV_COMPAT semtech_sx9500
10
11 #include <errno.h>
12
13 #include <zephyr/kernel.h>
14 #include <zephyr/drivers/i2c.h>
15 #include <zephyr/drivers/sensor.h>
16 #include <zephyr/init.h>
17 #include <zephyr/drivers/gpio.h>
18 #include <zephyr/sys/__assert.h>
19 #include <zephyr/logging/log.h>
20
21 #include "sx9500.h"
22
23 LOG_MODULE_REGISTER(SX9500, CONFIG_SENSOR_LOG_LEVEL);
24
25 static uint8_t sx9500_reg_defaults[] = {
26 /*
27 * First number is register address to write to. The chip
28 * auto-increments the address for subsequent values in a single
29 * write message.
30 */
31 SX9500_REG_PROX_CTRL1,
32
33 0x43, /* Shield enabled, small range. */
34 0x77, /* x8 gain, 167kHz frequency, finest resolution. */
35 0x40, /* Doze enabled, 2x scan period doze, no raw filter. */
36 0x30, /* Average threshold. */
37 0x0f, /* Debouncer off, lowest average negative filter,
38 * highest average positive filter.
39 */
40 0x0e, /* Proximity detection threshold: 280 */
41 0x00, /* No automatic compensation, compensate each pin
42 * independently, proximity hysteresis: 32, close
43 * debouncer off, far debouncer off.
44 */
45 0x00, /* No stuck timeout, no periodic compensation. */
46 };
47
sx9500_sample_fetch(const struct device * dev,enum sensor_channel chan)48 static int sx9500_sample_fetch(const struct device *dev,
49 enum sensor_channel chan)
50 {
51 struct sx9500_data *data = dev->data;
52 const struct sx9500_config *cfg = dev->config;
53
54 __ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_PROX);
55
56 return i2c_reg_read_byte_dt(&cfg->i2c, SX9500_REG_STAT, &data->prox_stat);
57 }
58
sx9500_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)59 static int sx9500_channel_get(const struct device *dev,
60 enum sensor_channel chan,
61 struct sensor_value *val)
62 {
63 struct sx9500_data *data = (struct sx9500_data *) dev->data;
64
65 __ASSERT_NO_MSG(chan == SENSOR_CHAN_PROX);
66
67 if (chan != SENSOR_CHAN_PROX) {
68 return -ENOTSUP;
69 }
70
71 val->val1 = !!(data->prox_stat &
72 (1 << (4 + CONFIG_SX9500_PROX_CHANNEL)));
73 val->val2 = 0;
74
75 return 0;
76 }
77
78 static DEVICE_API(sensor, sx9500_api_funcs) = {
79 .sample_fetch = sx9500_sample_fetch,
80 .channel_get = sx9500_channel_get,
81 #ifdef CONFIG_SX9500_TRIGGER
82 .trigger_set = sx9500_trigger_set,
83 #endif
84 };
85
sx9500_init_chip(const struct device * dev)86 static int sx9500_init_chip(const struct device *dev)
87 {
88 const struct sx9500_config *cfg = dev->config;
89 uint8_t val;
90
91 if (i2c_write_dt(&cfg->i2c, sx9500_reg_defaults,
92 sizeof(sx9500_reg_defaults))
93 < 0) {
94 return -EIO;
95 }
96
97 /* No interrupts active. We only activate them when an
98 * application registers a trigger.
99 */
100 if (i2c_reg_write_byte_dt(&cfg->i2c, SX9500_REG_IRQ_MSK, 0) < 0) {
101 return -EIO;
102 }
103
104 /* Read irq source reg to clear reset status. */
105 if (i2c_reg_read_byte_dt(&cfg->i2c, SX9500_REG_IRQ_SRC, &val) < 0) {
106 return -EIO;
107 }
108
109 return i2c_reg_write_byte_dt(&cfg->i2c, SX9500_REG_PROX_CTRL0,
110 1 << CONFIG_SX9500_PROX_CHANNEL);
111 }
112
sx9500_init(const struct device * dev)113 int sx9500_init(const struct device *dev)
114 {
115 const struct sx9500_config *cfg = dev->config;
116
117 if (!device_is_ready(cfg->i2c.bus)) {
118 LOG_ERR("Bus device is not ready");
119 return -ENODEV;
120 }
121
122 if (sx9500_init_chip(dev) < 0) {
123 LOG_DBG("sx9500: failed to initialize chip");
124 return -EINVAL;
125 }
126
127 #ifdef CONFIG_SX9500_TRIGGER
128 if (cfg->int_gpio.port) {
129 if (sx9500_setup_interrupt(dev) < 0) {
130 LOG_DBG("sx9500: failed to setup interrupt");
131 return -EINVAL;
132 }
133 }
134 #endif
135
136 return 0;
137 }
138
139 #define SX9500_DEFINE(inst) \
140 struct sx9500_data sx9500_data_##inst; \
141 \
142 static const struct sx9500_config sx9500_config_##inst = { \
143 .i2c = I2C_DT_SPEC_INST_GET(inst), \
144 IF_ENABLED(CONFIG_SX9500_TRIGGER, \
145 (.int_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, int_gpios, { 0 }),)) \
146 }; \
147 \
148 SENSOR_DEVICE_DT_INST_DEFINE(inst, sx9500_init, NULL, \
149 &sx9500_data_##inst, &sx9500_config_##inst, POST_KERNEL, \
150 CONFIG_SENSOR_INIT_PRIORITY, &sx9500_api_funcs); \
151
152 DT_INST_FOREACH_STATUS_OKAY(SX9500_DEFINE)
153