1 /* fdc2x1x.c - Driver for the Texas Instruments FDC2X1X */
2
3 /*
4 * Copyright (c) 2020 arithmetics.io
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9 #define DT_DRV_COMPAT ti_fdc2x1x
10
11 #include <zephyr/device.h>
12 #include <zephyr/pm/device.h>
13 #include <zephyr/sys/util.h>
14 #include <zephyr/logging/log.h>
15 #include <math.h>
16
17 #include "fdc2x1x.h"
18 #include <zephyr/drivers/sensor/fdc2x1x.h>
19
20 LOG_MODULE_REGISTER(FDC2X1X, CONFIG_SENSOR_LOG_LEVEL);
21
22 static int fdc2x1x_init_config(const struct device *dev);
23
24 /**
25 * Convert raw data to frequency (MHz).
26 * @param dev - The device structure.
27 * @param ch - Channel to convert the data from.
28 * @param freq - Calculated frequency value .
29 */
fdc2x1x_raw_to_freq(const struct device * dev,uint8_t ch,double * freq)30 static void fdc2x1x_raw_to_freq(const struct device *dev,
31 uint8_t ch, double *freq)
32 {
33 struct fdc2x1x_data *data = dev->data;
34 const struct fdc2x1x_config *cfg = dev->config;
35
36 if (data->fdc221x) {
37 *freq = (cfg->ch_cfg->fin_sel * (cfg->fref / 1000.0) *
38 data->channel_buf[ch]) / pow(2, 28);
39 } else {
40 *freq = cfg->ch_cfg->fin_sel * (cfg->fref / 1000.0) *
41 ((data->channel_buf[ch] / pow(2, 12 + cfg->output_gain)) +
42 (cfg->ch_cfg[ch].offset / pow(2, 16)));
43 }
44 }
45
46 /**
47 * Convert raw data to capacitance in picofarad (pF).
48 * Requires the previous conversion from raw to frequency.
49 * @param dev - The device structure.
50 * @param ch - Channel to convert the data from .
51 * @param freq - Frequency value
52 * @param capacitance - Calculated capacitance value
53 */
fdc2x1x_raw_to_capacitance(const struct device * dev,uint8_t ch,double freq,double * capacitance)54 static void fdc2x1x_raw_to_capacitance(const struct device *dev,
55 uint8_t ch, double freq, double *capacitance)
56 {
57 const struct fdc2x1x_config *cfg = dev->config;
58
59 *capacitance = 1 / ((cfg->ch_cfg->inductance / 1000000.0) *
60 pow((2 * PI * freq), 2));
61 }
62
63 /**
64 * Read/Write from device.
65 * @param dev - The device structure.
66 * @param reg - The register address. Use FDC2X1X_REG_READ(x) or
67 * FDC2X1X_REG_WRITE(x).
68 * @param data - The register data.
69 * @param length - Number of bytes being read
70 * @return 0 in case of success, negative error code otherwise.
71 */
fdc2x1x_bus_access(const struct device * dev,uint8_t reg,uint8_t * data,size_t length)72 static int fdc2x1x_bus_access(const struct device *dev, uint8_t reg,
73 uint8_t *data, size_t length)
74 {
75 const struct fdc2x1x_config *cfg = dev->config;
76
77 if (reg & FDC2X1X_READ) {
78 return i2c_burst_read_dt(&cfg->i2c, FDC2X1X_TO_I2C_REG(reg), data, length);
79 } else {
80 if (length != 2) {
81 return -EINVAL;
82 }
83
84 uint8_t buf[3];
85
86 buf[0] = FDC2X1X_TO_I2C_REG(reg);
87 memcpy(buf + 1, data, sizeof(uint16_t));
88
89 return i2c_write_dt(&cfg->i2c, buf, sizeof(buf));
90 }
91 }
92
93 /**
94 * Read (16 Bit) from device.
95 * @param dev - The device structure.
96 * @param reg_addr - The register address.
97 * @param reg_data - The register data.
98 * @return 0 in case of success, negative error code otherwise.
99 */
fdc2x1x_reg_read(const struct device * dev,uint8_t reg_addr,uint16_t * reg_data)100 static int fdc2x1x_reg_read(const struct device *dev,
101 uint8_t reg_addr,
102 uint16_t *reg_data)
103 {
104 uint8_t buf[2];
105 int ret;
106
107 ret = fdc2x1x_bus_access(dev, FDC2X1X_REG_READ(reg_addr), buf, 2);
108 *reg_data = ((uint16_t)buf[0] << 8) | buf[1];
109
110 return ret;
111 }
112
113 /**
114 * Write (16 Bit) to device.
115 * @param dev - The device structure.
116 * @param reg_addr - The register address.
117 * @param reg_data - The register data.
118 * @return 0 in case of success, negative error code otherwise.
119 */
fdc2x1x_reg_write(const struct device * dev,uint8_t reg_addr,uint16_t reg_data)120 static int fdc2x1x_reg_write(const struct device *dev,
121 uint8_t reg_addr,
122 uint16_t reg_data)
123 {
124 LOG_DBG("[0x%x] = 0x%x", reg_addr, reg_data);
125
126 uint8_t buf[2];
127
128 buf[0] = (uint8_t)(reg_data >> 8);
129 buf[1] = (uint8_t)reg_data;
130
131 return fdc2x1x_bus_access(dev, FDC2X1X_REG_WRITE(reg_addr), buf, 2);
132 }
133
134 /**
135 * I2C write (16 Bit) to device using a mask.
136 * @param dev - The device structure.
137 * @param reg_addr - The register address.
138 * @param mask - The mask.
139 * @param data - The register data.
140 * @return 0 in case of success, negative error code otherwise.
141 */
fdc2x1x_reg_write_mask(const struct device * dev,uint8_t reg_addr,uint16_t mask,uint16_t data)142 int fdc2x1x_reg_write_mask(const struct device *dev,
143 uint8_t reg_addr,
144 uint16_t mask,
145 uint16_t data)
146 {
147 int ret;
148 uint16_t tmp;
149
150 ret = fdc2x1x_reg_read(dev, reg_addr, &tmp);
151 if (ret) {
152 return ret;
153 }
154 LOG_DBG("read [0x%x] = 0x%x", reg_addr, tmp);
155 LOG_DBG("mask: 0x%x", mask);
156
157 tmp &= ~mask;
158 tmp |= data;
159
160 return fdc2x1x_reg_write(dev, reg_addr, tmp);
161 }
162
163 /**
164 * Set the Frequency Selection value of a specific channel.
165 * @param dev - The device structure.
166 * @param chx - Channel number.
167 * @param fin_sel - Frequency selection value.
168 * @return 0 in case of success, negative error code otherwise.
169 */
fdc2x1x_set_fin_sel(const struct device * dev,uint8_t chx,uint8_t fin_sel)170 static int fdc2x1x_set_fin_sel(const struct device *dev, uint8_t chx,
171 uint8_t fin_sel)
172 {
173 return fdc2x1x_reg_write_mask(dev,
174 FDC2X1X_CLOCK_DIVIDERS_CH0 + chx,
175 FDC2X1X_CLK_DIV_CHX_FIN_SEL_MSK,
176 FDC2X1X_CLK_DIV_CHX_FIN_SEL_SET(fin_sel));
177 }
178
179 /**
180 * Set the Reference Divider value of a specific channel.
181 * @param dev - The device structure.
182 * @param chx - Channel number.
183 * @param fref_div - Reference divider value.
184 * @return 0 in case of success, negative error code otherwise.
185 */
fdc2x1x_set_fref_divider(const struct device * dev,uint8_t chx,uint16_t fref_div)186 static int fdc2x1x_set_fref_divider(const struct device *dev, uint8_t chx,
187 uint16_t fref_div)
188 {
189 return fdc2x1x_reg_write_mask(dev,
190 FDC2X1X_CLOCK_DIVIDERS_CH0 + chx,
191 FDC2X1X_CLK_DIV_CHX_FREF_DIV_MSK,
192 FDC2X1X_CLK_DIV_CHX_FREF_DIV_SET(fref_div));
193 }
194
195 /**
196 * Set the Drive Current value of a specific channel.
197 * @param dev - The device structure.
198 * @param chx - Channel number.
199 * @param idrv - Sensor driver current.
200 * @return 0 in case of success, negative error code otherwise.
201 */
fdc2x1x_set_idrive(const struct device * dev,uint8_t chx,uint8_t idrv)202 static int fdc2x1x_set_idrive(const struct device *dev, uint8_t chx,
203 uint8_t idrv)
204 {
205 return fdc2x1x_reg_write_mask(dev,
206 FDC2X1X_DRIVE_CURRENT_CH0 + chx,
207 FDC2X1X_DRV_CURRENT_CHX_IDRIVE_MSK,
208 FDC2X1X_DRV_CURRENT_CHX_IDRIVE_SET(idrv));
209 }
210
211 /**
212 * Set the Conversion Settling value of a specific channel.
213 * @param dev - The device structure.
214 * @param chx - Channel number.
215 * @param settle_count - Settling time value.
216 * @return 0 in case of success, negative error code otherwise.
217 */
fdc2x1x_set_settle_count(const struct device * dev,uint8_t chx,uint16_t settle_count)218 static int fdc2x1x_set_settle_count(const struct device *dev, uint8_t chx,
219 uint16_t settle_count)
220 {
221 return fdc2x1x_reg_write(dev,
222 FDC2X1X_SETTLECOUNT_CH0 + chx, settle_count);
223 }
224
225 /**
226 * Set the Reference Count value of a specific channel.
227 * @param dev - The device structure.
228 * @param chx - Channel number.
229 * @param rcount - Reference count value.
230 * @return 0 in case of success, negative error code otherwise.
231 */
fdc2x1x_set_rcount(const struct device * dev,uint8_t chx,uint16_t rcount)232 static int fdc2x1x_set_rcount(const struct device *dev,
233 uint8_t chx, uint16_t rcount)
234 {
235 return fdc2x1x_reg_write(dev, FDC2X1X_RCOUNT_CH0 + chx, rcount);
236 }
237
238
239 /**
240 * Set the Offset value of a specific channel.
241 * @param dev - The device structure.
242 * @param chx - Channel number.
243 * @param offset - Offset value.
244 * @return 0 in case of success, negative error code otherwise.
245 */
fdc2x1x_set_offset(const struct device * dev,uint8_t chx,uint16_t offset)246 static int fdc2x1x_set_offset(const struct device *dev,
247 uint8_t chx, uint16_t offset)
248 {
249 return fdc2x1x_reg_write(dev, FDC2X1X_OFFSET_CH0 + chx, offset);
250 }
251
252 /**
253 * Set the Auto-Scan Mode.
254 * @param dev - The device structure.
255 * @param en - Enable/disable auto-scan mode.
256 * @return 0 in case of success, negative error code otherwise.
257 */
fdc2x1x_set_autoscan_mode(const struct device * dev,bool en)258 static int fdc2x1x_set_autoscan_mode(const struct device *dev, bool en)
259 {
260 return fdc2x1x_reg_write_mask(dev,
261 FDC2X1X_MUX_CONFIG,
262 FDC2X1X_MUX_CFG_AUTOSCAN_EN_MSK,
263 FDC2X1X_MUX_CFG_AUTOSCAN_EN_SET(en));
264 }
265
266 /**
267 * Set the Auto-Scan Sequence Configuration.
268 * @param dev - The device structure.
269 * @param rr_seq - Auto-Scan sequence value.
270 * @return 0 in case of success, negative error code otherwise.
271 */
fdc2x1x_set_rr_sequence(const struct device * dev,uint8_t rr_seq)272 static int fdc2x1x_set_rr_sequence(const struct device *dev, uint8_t rr_seq)
273 {
274 return fdc2x1x_reg_write_mask(dev,
275 FDC2X1X_MUX_CONFIG,
276 FDC2X1X_MUX_CFG_RR_SEQUENCE_MSK,
277 FDC2X1X_MUX_CFG_RR_SEQUENCE_SET(rr_seq));
278 }
279
280 /**
281 * Set the Input deglitch filter bandwidth.
282 * @param dev - The device structure.
283 * @param deglitch - Deglitch selection.
284 * @return 0 in case of success, negative error code otherwise.
285 */
fdc2x1x_set_deglitch(const struct device * dev,uint8_t deglitch)286 static int fdc2x1x_set_deglitch(const struct device *dev, uint8_t deglitch)
287 {
288 return fdc2x1x_reg_write_mask(dev,
289 FDC2X1X_MUX_CONFIG,
290 FDC2X1X_MUX_CFG_DEGLITCH_MSK,
291 FDC2X1X_MUX_CFG_DEGLITCH_SET(deglitch));
292 }
293
294 /**
295 * Set the Output gain control.
296 * @param dev - The device structure.
297 * @param gain - Output gain.
298 * @return 0 in case of success, negative error code otherwise.
299 */
fdc2x1x_set_output_gain(const struct device * dev,uint8_t gain)300 static int fdc2x1x_set_output_gain(const struct device *dev, uint8_t gain)
301 {
302 return fdc2x1x_reg_write_mask(dev,
303 FDC2X1X_RESET_DEV,
304 FDC2X1X_RESET_DEV_OUTPUT_GAIN_MSK,
305 FDC2X1X_RESET_DEV_OUTPUT_GAIN_SET(gain));
306 }
307
308 /**
309 * Set the Active Channel for single channel
310 * conversion if Auto-Scan Mode is disabled.
311 * @param dev - The device structure.
312 * @param ch - Active channel.
313 * @return 0 in case of success, negative error code otherwise.
314 */
fdc2x1x_set_active_channel(const struct device * dev,uint8_t ch)315 static int fdc2x1x_set_active_channel(const struct device *dev, uint8_t ch)
316 {
317 return fdc2x1x_reg_write_mask(dev,
318 FDC2X1X_CONFIG,
319 FDC2X1X_CFG_ACTIVE_CHAN_MSK,
320 FDC2X1X_CFG_ACTIVE_CHAN_SET(ch));
321 }
322
323 /**
324 * Set the Sensor Activation Mode Selection.
325 * @param dev - The device structure.
326 * @param act_sel - Sensor Activation Mode Selection.
327 * @return 0 in case of success, negative error code otherwise.
328 */
fdc2x1x_set_sensor_activate_sel(const struct device * dev,uint8_t act_sel)329 static int fdc2x1x_set_sensor_activate_sel(const struct device *dev,
330 uint8_t act_sel)
331 {
332 return fdc2x1x_reg_write_mask(dev,
333 FDC2X1X_CONFIG,
334 FDC2X1X_CFG_SENSOR_ACTIVATE_SEL_MSK,
335 FDC2X1X_CFG_SENSOR_ACTIVATE_SEL_SET(act_sel));
336 }
337
338 /**
339 * Set the Reference Frequency Source.
340 * @param dev - The device structure.
341 * @param clk_src - Clock source.
342 * @return 0 in case of success, negative error code otherwise.
343 */
fdc2x1x_set_ref_clk_src(const struct device * dev,uint8_t clk_src)344 static int fdc2x1x_set_ref_clk_src(const struct device *dev, uint8_t clk_src)
345 {
346 return fdc2x1x_reg_write_mask(dev,
347 FDC2X1X_CONFIG,
348 FDC2X1X_CFG_REF_CLK_SRC_MSK,
349 FDC2X1X_CFG_REF_CLK_SRC_SET(clk_src));
350 }
351
352 /**
353 * Set the Current Sensor Drive.
354 * @param dev - The device structure.
355 * @param cur_drv - Current Sensor Drive.
356 * @return 0 in case of success, negative error code otherwise.
357 */
fdc2x1x_set_current_drv(const struct device * dev,uint8_t cur_drv)358 static int fdc2x1x_set_current_drv(const struct device *dev, uint8_t cur_drv)
359 {
360 return fdc2x1x_reg_write_mask(dev,
361 FDC2X1X_CONFIG,
362 FDC2X1X_CFG_HIGH_CURRENT_DRV_MSK,
363 FDC2X1X_CFG_HIGH_CURRENT_DRV_SET(cur_drv));
364 }
365
366 /**
367 * Enable/disable the INTB-Pin interrupt assertion.
368 * @param dev - The device structure.
369 * @param enable - True = enable int assertion, false = disable int assertion.
370 * @return 0 in case of success, negative error code otherwise.
371 */
fdc2x1x_set_interrupt_pin(const struct device * dev,bool enable)372 int fdc2x1x_set_interrupt_pin(const struct device *dev, bool enable)
373 {
374 return fdc2x1x_reg_write_mask(dev,
375 FDC2X1X_CONFIG,
376 FDC2X1X_CFG_INTB_DIS_MSK,
377 FDC2X1X_CFG_INTB_DIS_SET(!enable));
378 }
379
380 /**
381 * Set the Operation Mode
382 * @param dev - The device structure.
383 * @param op_mode - Operation mode
384 * @return 0 in case of success, negative error code otherwise.
385 */
fdc2x1x_set_op_mode(const struct device * dev,enum fdc2x1x_op_mode op_mode)386 int fdc2x1x_set_op_mode(const struct device *dev,
387 enum fdc2x1x_op_mode op_mode)
388 {
389 return fdc2x1x_reg_write_mask(dev,
390 FDC2X1X_CONFIG,
391 FDC2X1X_CFG_SLEEP_SET_EN_MSK,
392 FDC2X1X_CFG_SLEEP_SET_EN_SET(op_mode));
393 }
394
395 /**
396 * Get the STATUS register data
397 * @param dev - The device structure.
398 * @param status - Data stored in the STATUS register
399 * @return 0 in case of success, negative error code otherwise.
400 */
fdc2x1x_get_status(const struct device * dev,uint16_t * status)401 int fdc2x1x_get_status(const struct device *dev, uint16_t *status)
402 {
403 return fdc2x1x_reg_read(dev, FDC2X1X_STATUS, status);
404 }
405
406 /**
407 * Reset the device.
408 * @param dev - The device structure.
409 * @return 0 in case of success, negative error code otherwise.
410 */
fdc2x1x_reset(const struct device * dev)411 static int fdc2x1x_reset(const struct device *dev)
412 {
413 int ret;
414
415 ret = fdc2x1x_reg_write_mask(dev,
416 FDC2X1X_RESET_DEV,
417 FDC2X1X_RESET_DEV_MSK,
418 FDC2X1X_RESET_DEV_SET(1));
419
420 return ret;
421 }
422
423 #ifdef CONFIG_PM_DEVICE
424 /**
425 * Reinitialize device after exiting shutdown mode
426 * @param dev - The device structure.
427 * @return 0 in case of success, negative error code otherwise.
428 */
fdc2x1x_restart(const struct device * dev)429 static int fdc2x1x_restart(const struct device *dev)
430 {
431 int ret;
432
433 k_sleep(K_MSEC(100));
434
435 ret = fdc2x1x_init_config(dev);
436 if (ret) {
437 LOG_ERR("Reinitializing failed");
438 return ret;
439 }
440
441 #ifdef CONFIG_FDC2X1X_TRIGGER
442 struct fdc2x1x_data *data = dev->data;
443
444 ret = fdc2x1x_reg_write_mask(dev, FDC2X1X_ERROR_CONFIG,
445 data->int_config, data->int_config);
446 if (ret) {
447 LOG_ERR("Reinitializing trigger failed");
448 return ret;
449 }
450 #endif
451
452 return 0;
453 }
454
455 /**
456 * Enable/disable Shutdown Mode
457 * @param dev - The device structure.
458 * @param enable - True = enable shutdown, false = disable shutdown
459 * @return 0 in case of success, negative error code otherwise.
460 */
fdc2x1x_set_shutdown(const struct device * dev,bool enable)461 static int fdc2x1x_set_shutdown(const struct device *dev, bool enable)
462 {
463 const struct fdc2x1x_config *cfg = dev->config;
464 int ret = 0;
465
466 gpio_pin_set_dt(&cfg->sd_gpio, enable);
467
468 if (!enable) {
469 ret = fdc2x1x_restart(dev);
470 }
471
472 return ret;
473 }
474
475 /**
476 * Set the Device Power Management State.
477 * @param dev - The device structure.
478 * @param pm_state - power management state
479 * @return 0 in case of success, negative error code otherwise.
480 */
fdc2x1x_device_pm_action(const struct device * dev,enum pm_device_action action)481 static int fdc2x1x_device_pm_action(const struct device *dev,
482 enum pm_device_action action)
483 {
484 int ret;
485 const struct fdc2x1x_config *cfg = dev->config;
486 enum pm_device_state curr_state;
487
488 (void)pm_device_state_get(dev, &curr_state);
489
490 switch (action) {
491 case PM_DEVICE_ACTION_RESUME:
492 if (curr_state == PM_DEVICE_STATE_OFF) {
493 ret = fdc2x1x_set_shutdown(dev, false);
494 if (ret) {
495 return ret;
496 }
497 }
498
499 ret = fdc2x1x_set_op_mode(dev, FDC2X1X_ACTIVE_MODE);
500 if (ret) {
501 return ret;
502 }
503
504 break;
505 case PM_DEVICE_ACTION_SUSPEND:
506 if (curr_state == PM_DEVICE_STATE_OFF) {
507 ret = fdc2x1x_set_shutdown(dev, false);
508 if (ret) {
509 return ret;
510 }
511 }
512 ret = fdc2x1x_set_op_mode(dev, FDC2X1X_SLEEP_MODE);
513 if (ret) {
514 return ret;
515 }
516
517 break;
518 case PM_DEVICE_ACTION_TURN_OFF:
519 if (cfg->sd_gpio.port->name) {
520 ret = fdc2x1x_set_shutdown(dev, true);
521 } else {
522 LOG_ERR("SD pin not defined");
523 ret = -ENOTSUP;
524 }
525 break;
526 default:
527 return -ENOTSUP;
528 }
529
530 return ret;
531 }
532 #endif
533
534 /**
535 * Set attributes for the device.
536 * @param dev - The device structure.
537 * @param chan - The sensor channel type.
538 * @param attr - The sensor attribute.
539 * @param value - The sensor attribute value.
540 * @return 0 in case of success, negative error code otherwise.
541 */
fdc2x1x_attr_set(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,const struct sensor_value * val)542 static int fdc2x1x_attr_set(const struct device *dev,
543 enum sensor_channel chan,
544 enum sensor_attribute attr,
545 const struct sensor_value *val)
546 {
547 return -ENOTSUP;
548 }
549
550 /**
551 * Read sensor data from the device.
552 * @param dev - The device structure.
553 * @param cap_data - The sensor value data.
554 * @return 0 in case of success, negative error code otherwise.
555 */
fdc2x1x_get_cap_data(const struct device * dev)556 static int fdc2x1x_get_cap_data(const struct device *dev)
557 {
558 uint8_t increment_steps;
559 int i;
560
561 const struct fdc2x1x_config *cfg = dev->config;
562 struct fdc2x1x_data *data = dev->data;
563 uint8_t reg_addr = FDC2X1X_DATA_CH0;
564 uint8_t buf_size = cfg->num_channels;
565
566 if (data->fdc221x) {
567 buf_size *= 2;
568 increment_steps = 1;
569 } else {
570 increment_steps = 2;
571 }
572
573 uint16_t buf[buf_size];
574
575 #ifdef CONFIG_FDC2X1X_TRIGGER_NONE
576 uint16_t status;
577
578 do {
579 fdc2x1x_get_status(dev, &status);
580 } while (!(FDC2X1X_STATUS_DRDY(status)));
581 #endif
582
583 for (i = 0; i < buf_size; i++) {
584 if (fdc2x1x_reg_read(dev, reg_addr, &buf[i]) < 0) {
585 LOG_ERR("Failed to read reg 0x%x", reg_addr);
586 return -EIO;
587 }
588 reg_addr += increment_steps;
589 }
590
591 for (i = 0; i < cfg->num_channels; i++) {
592 if (data->fdc221x) {
593 data->channel_buf[i] = buf[i * 2] << 16 | buf[i * 2 + 1];
594 } else {
595 data->channel_buf[i] = buf[i];
596 }
597 }
598
599 return 0;
600 }
601
602 /**
603 * Fetch sensor data from the device.
604 * @param dev - The device structure.
605 * @param chan - The sensor channel type.
606 * @return 0 in case of success, negative error code otherwise.
607 */
fdc2x1x_sample_fetch(const struct device * dev,enum sensor_channel chan)608 static int fdc2x1x_sample_fetch(const struct device *dev,
609 enum sensor_channel chan)
610 {
611 #ifdef CONFIG_PM_DEVICE
612 enum pm_device_state state;
613
614 (void)pm_device_state_get(dev, &state);
615 if (state != PM_DEVICE_STATE_ACTIVE) {
616 LOG_ERR("Sample fetch failed, device is not in active mode");
617 return -ENXIO;
618 }
619 #endif
620
621 return fdc2x1x_get_cap_data(dev);
622 }
623
624 /**
625 * Get sensor channel value from the device.
626 * @param dev - The device structure.
627 * @param chan - The sensor channel type.
628 * @param val - The sensor channel value.
629 * @return 0 in case of success, negative error code otherwise.
630 */
fdc2x1x_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)631 static int fdc2x1x_channel_get(const struct device *dev,
632 enum sensor_channel chan,
633 struct sensor_value *val)
634 {
635 const struct fdc2x1x_config *cfg = dev->config;
636 double ch_data;
637
638 switch ((int16_t)chan) {
639 case SENSOR_CHAN_FDC2X1X_FREQ_CH0:
640 fdc2x1x_raw_to_freq(dev, 0, &ch_data);
641 val->val1 = (uint32_t)ch_data;
642 val->val2 = ((uint32_t)(ch_data * 1000000)) % 1000000;
643 break;
644 case SENSOR_CHAN_FDC2X1X_FREQ_CH1:
645 if (cfg->num_channels >= 2) {
646 fdc2x1x_raw_to_freq(dev, 1, &ch_data);
647 val->val1 = (uint32_t)ch_data;
648 val->val2 = ((uint32_t)(ch_data * 1000000)) % 1000000;
649 } else {
650 LOG_ERR("CH1 not defined.");
651 return -ENOTSUP;
652 }
653 break;
654 case SENSOR_CHAN_FDC2X1X_FREQ_CH2:
655 if (cfg->num_channels >= 3) {
656 fdc2x1x_raw_to_freq(dev, 2, &ch_data);
657 val->val1 = (uint32_t)ch_data;
658 val->val2 = ((uint32_t)(ch_data * 1000000)) % 1000000;
659 } else {
660 LOG_ERR("CH2 not selected or not supported by device.");
661 return -ENOTSUP;
662 }
663 break;
664 case SENSOR_CHAN_FDC2X1X_FREQ_CH3:
665 if (cfg->num_channels == 4) {
666 fdc2x1x_raw_to_freq(dev, 3, &ch_data);
667 val->val1 = (uint32_t)ch_data;
668 val->val2 = ((uint32_t)(ch_data * 1000000)) % 1000000;
669 } else {
670 LOG_ERR("CH3 not selected or not supported by device.");
671 return -ENOTSUP;
672 }
673 break;
674 case SENSOR_CHAN_FDC2X1X_CAPACITANCE_CH0:
675 fdc2x1x_raw_to_freq(dev, 0, &ch_data);
676 fdc2x1x_raw_to_capacitance(dev, 0, ch_data, &ch_data);
677 val->val1 = (uint32_t)ch_data;
678 val->val2 = ((uint32_t)(ch_data * 1000000)) % 1000000;
679 break;
680 case SENSOR_CHAN_FDC2X1X_CAPACITANCE_CH1:
681 if (cfg->num_channels >= 2) {
682 fdc2x1x_raw_to_freq(dev, 1, &ch_data);
683 fdc2x1x_raw_to_capacitance(dev, 1, ch_data, &ch_data);
684 val->val1 = (uint32_t)ch_data;
685 val->val2 = ((uint32_t)(ch_data * 1000000)) % 1000000;
686 } else {
687 LOG_ERR("CH1 not selected or not supported by device.");
688 return -ENOTSUP;
689 }
690 break;
691 case SENSOR_CHAN_FDC2X1X_CAPACITANCE_CH2:
692 if (cfg->num_channels >= 3) {
693 fdc2x1x_raw_to_freq(dev, 2, &ch_data);
694 fdc2x1x_raw_to_capacitance(dev, 2, ch_data, &ch_data);
695 val->val1 = (uint32_t)ch_data;
696 val->val2 = ((uint32_t)(ch_data * 1000000)) % 1000000;
697 } else {
698 LOG_ERR("CH3 not selected or not supported by device.");
699 return -ENOTSUP;
700 }
701 break;
702 case SENSOR_CHAN_FDC2X1X_CAPACITANCE_CH3:
703 if (cfg->num_channels >= 4) {
704 fdc2x1x_raw_to_freq(dev, 3, &ch_data);
705 fdc2x1x_raw_to_capacitance(dev, 3, ch_data, &ch_data);
706 val->val1 = (uint32_t)ch_data;
707 val->val2 = ((uint32_t)(ch_data * 1000000)) % 1000000;
708 } else {
709 LOG_ERR("CH3 not selected or not supported by device.");
710 return -ENOTSUP;
711 }
712 break;
713 default:
714 LOG_ERR("Channel type not supported.");
715 return -ENOTSUP;
716 }
717
718 return 0;
719 }
720
721 static DEVICE_API(sensor, fdc2x1x_api_funcs) = {
722 .attr_set = fdc2x1x_attr_set,
723 .sample_fetch = fdc2x1x_sample_fetch,
724 .channel_get = fdc2x1x_channel_get,
725 #ifdef CONFIG_FDC2X1X_TRIGGER
726 .trigger_set = fdc2x1x_trigger_set,
727 #endif
728 };
729
fdc2x1x_init_config(const struct device * dev)730 static int fdc2x1x_init_config(const struct device *dev)
731 {
732 int ret;
733 int ch;
734 const struct fdc2x1x_config *cfg = dev->config;
735 struct fdc2x1x_data *data = dev->data;
736
737 /* Channel specific settings */
738 for (ch = 0; ch < cfg->num_channels; ch++) {
739 ret = fdc2x1x_set_fin_sel(dev, ch, cfg->ch_cfg[ch].fin_sel);
740 if (ret) {
741 return ret;
742 }
743
744 ret = fdc2x1x_set_fref_divider(dev, ch,
745 cfg->ch_cfg[ch].fref_divider);
746 if (ret) {
747 return ret;
748 }
749
750 ret = fdc2x1x_set_idrive(dev, ch, cfg->ch_cfg[ch].idrive);
751 if (ret) {
752 return ret;
753 }
754
755 ret = fdc2x1x_set_settle_count(dev, ch,
756 cfg->ch_cfg[ch].settle_count);
757 if (ret) {
758 return ret;
759 }
760
761 ret = fdc2x1x_set_rcount(dev, ch, cfg->ch_cfg[ch].rcount);
762 if (ret) {
763 return ret;
764 }
765
766 if (!data->fdc221x) {
767 ret = fdc2x1x_set_offset(dev, ch,
768 cfg->ch_cfg[ch].offset);
769 if (ret) {
770 return ret;
771 }
772 }
773 }
774
775 ret = fdc2x1x_set_autoscan_mode(dev, cfg->autoscan_en);
776 if (ret) {
777 return ret;
778 }
779
780 ret = fdc2x1x_set_rr_sequence(dev, cfg->rr_sequence);
781 if (ret) {
782 return ret;
783 }
784
785 ret = fdc2x1x_set_deglitch(dev, cfg->deglitch);
786 if (ret) {
787 return ret;
788 }
789
790 if (!data->fdc221x) {
791 ret = fdc2x1x_set_output_gain(dev, cfg->output_gain);
792 if (ret) {
793 return ret;
794 }
795 }
796
797 ret = fdc2x1x_set_active_channel(dev, cfg->active_channel);
798 if (ret) {
799 return ret;
800 }
801
802 ret = fdc2x1x_set_sensor_activate_sel(dev, cfg->sensor_activate_sel);
803 if (ret) {
804 return ret;
805 }
806
807 ret = fdc2x1x_set_ref_clk_src(dev, cfg->clk_src);
808 if (ret) {
809 return ret;
810 }
811
812 #ifdef CONFIG_FDC2X1X_TRIGGER_NONE
813 /* Enable Data Ready Flag to poll for new measurement */
814 ret = fdc2x1x_reg_write_mask(dev, FDC2X1X_ERROR_CONFIG,
815 FDC2X1X_ERROR_CONFIG_DRDY_2INT_MSK,
816 FDC2X1X_ERROR_CONFIG_DRDY_2INT_SET(1));
817 if (ret) {
818 return ret;
819 }
820
821 /* INTB asserts by default, so disable it here */
822 ret = fdc2x1x_set_interrupt_pin(dev, false);
823 if (ret) {
824 return ret;
825 }
826 #endif
827
828 ret = fdc2x1x_set_current_drv(dev, cfg->current_drv);
829 if (ret) {
830 return ret;
831 }
832
833 return 0;
834 }
835
836 /**
837 * Probe device (Check if it is the correct device).
838 * @param dev - The device structure.
839 * @return 0 in case of success, negative error code otherwise.
840 */
fdc2x1x_probe(const struct device * dev)841 static int fdc2x1x_probe(const struct device *dev)
842 {
843 struct fdc2x1x_data *data = dev->data;
844 uint16_t dev_id, man_id;
845
846 if (fdc2x1x_reg_read(dev, FDC2X1X_DEVICE_ID, &dev_id) < 0) {
847 LOG_ERR("Failed to read device id");
848 return -EIO;
849 }
850
851
852 if (dev_id == FDC2X1X_DEVICE_ID_VAL_28BIT) {
853 data->fdc221x = true;
854 } else if (dev_id == FDC2X1X_DEVICE_ID_VAL) {
855 data->fdc221x = false;
856 } else {
857 LOG_ERR("Wrong device id");
858 return -ENODEV;
859 }
860
861 if (data->fdc221x) {
862 printk("is 28bit\n");
863 } else {
864 printk("is 12bit\n");
865 }
866
867 if (fdc2x1x_reg_read(dev, FDC2X1X_MANUFACTURER_ID, &man_id) < 0) {
868 LOG_ERR("Failed to read manufacturer id");
869 return -EIO;
870 }
871
872 if (man_id != FDC2X1X_MANUFACTURER_ID_VAL) {
873 LOG_ERR("Wrong manufacturer id");
874 return -ENODEV;
875 }
876
877 return 0;
878 }
879
880 /**
881 * Initialize the SD-Pin.
882 * @param dev - The device structure.
883 * @return 0 in case of success, negative error code otherwise.
884 */
fdc2x1x_init_sd_pin(const struct device * dev)885 static int fdc2x1x_init_sd_pin(const struct device *dev)
886 {
887 const struct fdc2x1x_config *cfg = dev->config;
888
889 if (!gpio_is_ready_dt(&cfg->sd_gpio)) {
890 LOG_ERR("%s: sd_gpio device not ready", cfg->sd_gpio.port->name);
891 return -ENODEV;
892 }
893
894 gpio_pin_configure_dt(&cfg->sd_gpio, GPIO_OUTPUT_INACTIVE);
895
896 return 0;
897 }
898
899 /**
900 * Initialization of the device.
901 * @param dev - The device structure.
902 * @return 0 in case of success, negative error code otherwise.
903 */
fdc2x1x_init(const struct device * dev)904 static int fdc2x1x_init(const struct device *dev)
905 {
906 const struct fdc2x1x_config *cfg = dev->config;
907 uint8_t ch_supported;
908
909 if (cfg->fdc2x14) {
910 ch_supported = 4;
911 } else {
912 ch_supported = 2;
913 }
914
915 if (cfg->num_channels == 0) {
916 LOG_ERR("No channel nodes found");
917 return -EINVAL;
918 } else if (cfg->num_channels > ch_supported) {
919 LOG_ERR("Amount of channels not supported by this device");
920 return -EINVAL;
921 }
922
923 if (cfg->sd_gpio.port->name) {
924 if (fdc2x1x_init_sd_pin(dev) < 0) {
925 return -ENODEV;
926 }
927 }
928
929 if (!device_is_ready(cfg->i2c.bus)) {
930 LOG_ERR("I2C bus device not ready");
931 return -ENODEV;
932 }
933
934 if (fdc2x1x_probe(dev) < 0) {
935 return -ENODEV;
936 }
937
938 if (fdc2x1x_reset(dev) < 0) {
939 return -EIO;
940 }
941
942 if (fdc2x1x_init_config(dev) < 0) {
943 return -EIO;
944 }
945
946 if (fdc2x1x_set_op_mode(dev, FDC2X1X_ACTIVE_MODE) < 0) {
947 return -EIO;
948 }
949
950 #ifdef CONFIG_FDC2X1X_TRIGGER
951 if (fdc2x1x_init_interrupt(dev) < 0) {
952 LOG_ERR("Failed to initialize interrupt!");
953 return -EIO;
954 }
955 #endif
956
957 return 0;
958 }
959
960 #define FDC2X1X_SD_PROPS(n) \
961 .sd_gpio = GPIO_DT_SPEC_INST_GET(n, sd_gpios), \
962
963 #define FDC2X1X_SD(n) \
964 IF_ENABLED(DT_INST_NODE_HAS_PROP(n, sd_gpios), \
965 (FDC2X1X_SD_PROPS(n)))
966
967 #define FDC2X1X_INTB_PROPS(n) \
968 .intb_gpio = GPIO_DT_SPEC_INST_GET(n, intb_gpios), \
969
970 #define FDC2X1X_INTB(n) \
971 IF_ENABLED(CONFIG_FDC2X1X_TRIGGER, \
972 (FDC2X1X_INTB_PROPS(n)))
973
974 #define FDC2X1X_CH_CFG_INIT(ch) \
975 { \
976 .rcount = DT_PROP(ch, rcount), \
977 .offset = DT_PROP(ch, offset), \
978 .settle_count = DT_PROP(ch, settlecount), \
979 .fref_divider = DT_PROP(ch, fref_divider), \
980 .idrive = DT_PROP(ch, idrive), \
981 .fin_sel = DT_PROP(ch, fin_sel), \
982 .inductance = DT_PROP(ch, inductance), \
983 },
984
985 #define FDC2X1X_CHANNEL_BUF_INIT(ch) 0,
986
987 #define FDC2X1X_INIT(n) \
988 static uint32_t fdc2x1x_sample_buf_##n[] = { \
989 DT_INST_FOREACH_CHILD(n, FDC2X1X_CHANNEL_BUF_INIT) \
990 }; \
991 \
992 static struct fdc2x1x_data fdc2x1x_data_##n = { \
993 .channel_buf = fdc2x1x_sample_buf_##n, \
994 }; \
995 \
996 const struct fdc2x1x_chx_config ch_cfg_##n[] = { \
997 DT_INST_FOREACH_CHILD(n, FDC2X1X_CH_CFG_INIT) \
998 }; \
999 \
1000 static const struct fdc2x1x_config fdc2x1x_config_##n = { \
1001 .i2c = I2C_DT_SPEC_INST_GET(n), \
1002 .fdc2x14 = DT_INST_PROP(n, fdc2x14), \
1003 .autoscan_en = DT_INST_PROP(n, autoscan), \
1004 .rr_sequence = DT_INST_PROP(n, rr_sequence), \
1005 .active_channel = DT_INST_PROP(n, active_channel), \
1006 .deglitch = DT_INST_PROP(n, deglitch), \
1007 .sensor_activate_sel = \
1008 DT_INST_ENUM_IDX(n, sensor_activate_sel), \
1009 .clk_src = DT_INST_ENUM_IDX(n, ref_clk_src), \
1010 .current_drv = DT_INST_ENUM_IDX(n, current_drive), \
1011 .output_gain = DT_INST_PROP(n, output_gain), \
1012 .ch_cfg = ch_cfg_##n, \
1013 .num_channels = ARRAY_SIZE(fdc2x1x_sample_buf_##n), \
1014 .fref = DT_INST_PROP(n, fref), \
1015 FDC2X1X_SD(n) \
1016 FDC2X1X_INTB(n) \
1017 }; \
1018 \
1019 PM_DEVICE_DT_INST_DEFINE(n, fdc2x1x_device_pm_action); \
1020 \
1021 SENSOR_DEVICE_DT_INST_DEFINE(n, \
1022 fdc2x1x_init, \
1023 PM_DEVICE_DT_INST_GET(n), \
1024 &fdc2x1x_data_##n, \
1025 &fdc2x1x_config_##n, \
1026 POST_KERNEL, \
1027 CONFIG_SENSOR_INIT_PRIORITY, \
1028 &fdc2x1x_api_funcs);
1029
1030 DT_INST_FOREACH_STATUS_OKAY(FDC2X1X_INIT)
1031