1 // SPDX-License-Identifier: GPL-2.0+
3 * cc2.c - Support for the Amphenol ChipCap 2 relative humidity, temperature sensor
5 * Part numbers supported:
6 * CC2D23, CC2D23S, CC2D25, CC2D25S, CC2D33, CC2D33S, CC2D35, CC2D35S
8 * Author: Javier Carrasco <javier.carrasco.cruz@gmail.com>
10 * Datasheet and application notes:
11 * https://www.amphenol-sensors.com/en/telaire/humidity/527-humidity-sensors/3095-chipcap-2
14 #include <linux/bitfield.h>
15 #include <linux/bits.h>
16 #include <linux/completion.h>
17 #include <linux/delay.h>
18 #include <linux/hwmon.h>
19 #include <linux/i2c.h>
20 #include <linux/interrupt.h>
21 #include <linux/irq.h>
22 #include <linux/module.h>
23 #include <linux/regulator/consumer.h>
25 #define CC2_START_CM 0xA0
26 #define CC2_START_NOM 0x80
27 #define CC2_R_ALARM_H_ON 0x18
28 #define CC2_R_ALARM_H_OFF 0x19
29 #define CC2_R_ALARM_L_ON 0x1A
30 #define CC2_R_ALARM_L_OFF 0x1B
31 #define CC2_RW_OFFSET 0x40
32 #define CC2_W_ALARM_H_ON (CC2_R_ALARM_H_ON + CC2_RW_OFFSET)
33 #define CC2_W_ALARM_H_OFF (CC2_R_ALARM_H_OFF + CC2_RW_OFFSET)
34 #define CC2_W_ALARM_L_ON (CC2_R_ALARM_L_ON + CC2_RW_OFFSET)
35 #define CC2_W_ALARM_L_OFF (CC2_R_ALARM_L_OFF + CC2_RW_OFFSET)
37 #define CC2_STATUS_FIELD GENMASK(7, 6)
38 #define CC2_STATUS_VALID_DATA 0x00
39 #define CC2_STATUS_STALE_DATA 0x01
40 #define CC2_STATUS_CMD_MODE 0x02
42 #define CC2_RESPONSE_FIELD GENMASK(1, 0)
43 #define CC2_RESPONSE_BUSY 0x00
44 #define CC2_RESPONSE_ACK 0x01
45 #define CC2_RESPONSE_NACK 0x02
47 #define CC2_ERR_CORR_EEPROM BIT(2)
48 #define CC2_ERR_UNCORR_EEPROM BIT(3)
49 #define CC2_ERR_RAM_PARITY BIT(4)
50 #define CC2_ERR_CONFIG_LOAD BIT(5)
52 #define CC2_EEPROM_SIZE 10
53 #define CC2_EEPROM_DATA_LEN 3
54 #define CC2_MEASUREMENT_DATA_LEN 4
56 #define CC2_RH_DATA_FIELD GENMASK(13, 0)
58 /* ensure clean off -> on transitions */
59 #define CC2_POWER_CYCLE_MS 80
61 #define CC2_STARTUP_TO_DATA_MS 55
62 #define CC2_RESP_START_CM_US 100
63 #define CC2_RESP_EEPROM_R_US 100
64 #define CC2_RESP_EEPROM_W_MS 12
65 #define CC2_STARTUP_TIME_US 1250
67 #define CC2_RH_MAX (100 * 1000U)
69 #define CC2_CM_RETRIES 5
71 struct cc2_rh_alarm_info
{
74 bool low_alarm_visible
;
75 bool high_alarm_visible
;
79 struct cc2_rh_alarm_info rh_alarm
;
80 struct completion complete
;
82 struct i2c_client
*client
;
83 struct mutex dev_access_lock
; /* device access lock */
84 struct regulator
*regulator
;
97 /* %RH as a per cent mille from a register value */
98 static long cc2_rh_convert(u16 data
)
100 unsigned long tmp
= (data
& CC2_RH_DATA_FIELD
) * CC2_RH_MAX
;
102 return tmp
/ ((1 << 14) - 1);
105 /* convert %RH to a register value */
106 static u16
cc2_rh_to_reg(long data
)
108 return data
* ((1 << 14) - 1) / CC2_RH_MAX
;
111 /* temperature in milli degrees celsius from a register value */
112 static long cc2_temp_convert(u16 data
)
114 unsigned long tmp
= ((data
>> 2) * 165 * 1000U) / ((1 << 14) - 1);
116 return tmp
- 40 * 1000U;
119 static int cc2_enable(struct cc2_data
*data
)
123 /* exclusive regulator, check in case a disable failed */
124 if (regulator_is_enabled(data
->regulator
))
127 /* clear any pending completion */
128 try_wait_for_completion(&data
->complete
);
130 ret
= regulator_enable(data
->regulator
);
134 usleep_range(CC2_STARTUP_TIME_US
, CC2_STARTUP_TIME_US
+ 125);
136 data
->process_irqs
= true;
141 static void cc2_disable(struct cc2_data
*data
)
145 /* ignore alarms triggered by voltage toggling when powering up */
146 data
->process_irqs
= false;
148 /* exclusive regulator, check in case an enable failed */
149 if (regulator_is_enabled(data
->regulator
)) {
150 err
= regulator_disable(data
->regulator
);
152 dev_dbg(&data
->client
->dev
, "Failed to disable device");
156 static int cc2_cmd_response_diagnostic(struct device
*dev
, u8 status
)
160 if (FIELD_GET(CC2_STATUS_FIELD
, status
) != CC2_STATUS_CMD_MODE
) {
161 dev_dbg(dev
, "Command sent out of command window\n");
165 resp
= FIELD_GET(CC2_RESPONSE_FIELD
, status
);
167 case CC2_RESPONSE_ACK
:
169 case CC2_RESPONSE_BUSY
:
171 case CC2_RESPONSE_NACK
:
172 if (resp
& CC2_ERR_CORR_EEPROM
)
173 dev_dbg(dev
, "Command failed: corrected EEPROM\n");
174 if (resp
& CC2_ERR_UNCORR_EEPROM
)
175 dev_dbg(dev
, "Command failed: uncorrected EEPROM\n");
176 if (resp
& CC2_ERR_RAM_PARITY
)
177 dev_dbg(dev
, "Command failed: RAM parity\n");
178 if (resp
& CC2_ERR_RAM_PARITY
)
179 dev_dbg(dev
, "Command failed: configuration error\n");
182 dev_dbg(dev
, "Unknown command reply\n");
187 static int cc2_read_command_status(struct i2c_client
*client
)
192 ret
= i2c_master_recv(client
, &status
, 1);
194 ret
= ret
< 0 ? ret
: -EIO
;
198 return cc2_cmd_response_diagnostic(&client
->dev
, status
);
202 * The command mode is only accessible after sending the START_CM command in the
203 * first 10 ms after power-up. Only in case the command window is missed,
204 * CC2_CM_RETRIES retries are attempted before giving up and returning an error.
206 static int cc2_command_mode_start(struct cc2_data
*data
)
208 unsigned long timeout
;
211 for (i
= 0; i
< CC2_CM_RETRIES
; i
++) {
212 ret
= cc2_enable(data
);
216 ret
= i2c_smbus_write_word_data(data
->client
, CC2_START_CM
, 0);
220 if (data
->irq_ready
> 0) {
221 timeout
= usecs_to_jiffies(2 * CC2_RESP_START_CM_US
);
222 ret
= wait_for_completion_timeout(&data
->complete
,
227 usleep_range(CC2_RESP_START_CM_US
,
228 2 * CC2_RESP_START_CM_US
);
230 ret
= cc2_read_command_status(data
->client
);
231 if (ret
!= -ETIMEDOUT
|| i
== CC2_CM_RETRIES
)
234 /* command window missed, prepare for a retry */
236 msleep(CC2_POWER_CYCLE_MS
);
242 /* Sending a Start_NOM command finishes the command mode immediately with no
243 * reply and the device enters normal operation mode
245 static int cc2_command_mode_finish(struct cc2_data
*data
)
249 ret
= i2c_smbus_write_word_data(data
->client
, CC2_START_NOM
, 0);
256 static int cc2_write_reg(struct cc2_data
*data
, u8 reg
, u16 val
)
258 unsigned long timeout
;
261 ret
= cc2_command_mode_start(data
);
266 ret
= i2c_smbus_write_word_data(data
->client
, reg
, val
);
270 if (data
->irq_ready
> 0) {
271 timeout
= msecs_to_jiffies(2 * CC2_RESP_EEPROM_W_MS
);
272 ret
= wait_for_completion_timeout(&data
->complete
, timeout
);
278 msleep(CC2_RESP_EEPROM_W_MS
);
281 ret
= cc2_read_command_status(data
->client
);
289 static int cc2_read_reg(struct cc2_data
*data
, u8 reg
, u16
*val
)
291 u8 buf
[CC2_EEPROM_DATA_LEN
];
292 unsigned long timeout
;
295 ret
= cc2_command_mode_start(data
);
299 ret
= i2c_smbus_write_word_data(data
->client
, reg
, 0);
303 if (data
->irq_ready
> 0) {
304 timeout
= usecs_to_jiffies(2 * CC2_RESP_EEPROM_R_US
);
305 ret
= wait_for_completion_timeout(&data
->complete
, timeout
);
310 usleep_range(CC2_RESP_EEPROM_R_US
, CC2_RESP_EEPROM_R_US
+ 10);
312 ret
= i2c_master_recv(data
->client
, buf
, CC2_EEPROM_DATA_LEN
);
313 if (ret
!= CC2_EEPROM_DATA_LEN
)
314 return ret
< 0 ? ret
: -EIO
;
316 *val
= be16_to_cpup((__be16
*)&buf
[1]);
318 return cc2_read_command_status(data
->client
);
321 static int cc2_get_reg_val(struct cc2_data
*data
, u8 reg
, long *val
)
326 ret
= cc2_read_reg(data
, reg
, ®_val
);
328 *val
= cc2_rh_convert(reg_val
);
335 static int cc2_data_fetch(struct i2c_client
*client
,
336 enum hwmon_sensor_types type
, long *val
)
338 u8 data
[CC2_MEASUREMENT_DATA_LEN
];
342 ret
= i2c_master_recv(client
, data
, CC2_MEASUREMENT_DATA_LEN
);
343 if (ret
!= CC2_MEASUREMENT_DATA_LEN
) {
344 ret
= ret
< 0 ? ret
: -EIO
;
347 status
= FIELD_GET(CC2_STATUS_FIELD
, data
[0]);
348 if (status
== CC2_STATUS_STALE_DATA
)
351 if (status
!= CC2_STATUS_VALID_DATA
)
356 *val
= cc2_rh_convert(be16_to_cpup((__be16
*)&data
[0]));
359 *val
= cc2_temp_convert(be16_to_cpup((__be16
*)&data
[2]));
368 static int cc2_read_measurement(struct cc2_data
*data
,
369 enum hwmon_sensor_types type
, long *val
)
371 unsigned long timeout
;
374 if (data
->irq_ready
> 0) {
375 timeout
= msecs_to_jiffies(CC2_STARTUP_TO_DATA_MS
* 2);
376 ret
= wait_for_completion_timeout(&data
->complete
, timeout
);
381 msleep(CC2_STARTUP_TO_DATA_MS
);
384 ret
= cc2_data_fetch(data
->client
, type
, val
);
390 * A measurement requires enabling the device, waiting for the automatic
391 * measurement to finish, reading the measurement data and disabling the device
394 static int cc2_measurement(struct cc2_data
*data
, enum hwmon_sensor_types type
,
399 ret
= cc2_enable(data
);
403 ret
= cc2_read_measurement(data
, type
, val
);
411 * In order to check alarm status, the corresponding ALARM_OFF (hysteresis)
412 * register must be read and a new measurement must be carried out to trigger
413 * the alarm signals. Given that the device carries out a measurement after
414 * exiting the command mode, there is no need to force two power-up sequences.
415 * Instead, a NOM command is sent and the device is disabled after the
416 * measurement is read.
418 static int cc2_read_hyst_and_measure(struct cc2_data
*data
, u8 reg
,
419 long *hyst
, long *measurement
)
424 ret
= cc2_read_reg(data
, reg
, ®_val
);
428 *hyst
= cc2_rh_convert(reg_val
);
430 ret
= cc2_command_mode_finish(data
);
434 ret
= cc2_read_measurement(data
, hwmon_humidity
, measurement
);
442 static umode_t
cc2_is_visible(const void *data
, enum hwmon_sensor_types type
,
443 u32 attr
, int channel
)
445 const struct cc2_data
*cc2
= data
;
450 case hwmon_humidity_input
:
452 case hwmon_humidity_min_alarm
:
453 return cc2
->rh_alarm
.low_alarm_visible
? 0444 : 0;
454 case hwmon_humidity_max_alarm
:
455 return cc2
->rh_alarm
.high_alarm_visible
? 0444 : 0;
456 case hwmon_humidity_min
:
457 case hwmon_humidity_min_hyst
:
458 return cc2
->rh_alarm
.low_alarm_visible
? 0644 : 0;
459 case hwmon_humidity_max
:
460 case hwmon_humidity_max_hyst
:
461 return cc2
->rh_alarm
.high_alarm_visible
? 0644 : 0;
467 case hwmon_temp_input
:
479 static irqreturn_t
cc2_ready_interrupt(int irq
, void *data
)
481 struct cc2_data
*cc2
= data
;
483 if (cc2
->process_irqs
)
484 complete(&cc2
->complete
);
489 static irqreturn_t
cc2_low_interrupt(int irq
, void *data
)
491 struct cc2_data
*cc2
= data
;
493 if (cc2
->process_irqs
) {
494 hwmon_notify_event(cc2
->hwmon
, hwmon_humidity
,
495 hwmon_humidity_min_alarm
, CC2_CHAN_HUMIDITY
);
496 cc2
->rh_alarm
.low_alarm
= true;
502 static irqreturn_t
cc2_high_interrupt(int irq
, void *data
)
504 struct cc2_data
*cc2
= data
;
506 if (cc2
->process_irqs
) {
507 hwmon_notify_event(cc2
->hwmon
, hwmon_humidity
,
508 hwmon_humidity_max_alarm
, CC2_CHAN_HUMIDITY
);
509 cc2
->rh_alarm
.high_alarm
= true;
515 static int cc2_humidity_min_alarm_status(struct cc2_data
*data
, long *val
)
517 long measurement
, min_hyst
;
520 ret
= cc2_read_hyst_and_measure(data
, CC2_R_ALARM_L_OFF
, &min_hyst
,
525 if (data
->rh_alarm
.low_alarm
) {
526 *val
= (measurement
< min_hyst
) ? 1 : 0;
527 data
->rh_alarm
.low_alarm
= *val
;
535 static int cc2_humidity_max_alarm_status(struct cc2_data
*data
, long *val
)
537 long measurement
, max_hyst
;
540 ret
= cc2_read_hyst_and_measure(data
, CC2_R_ALARM_H_OFF
, &max_hyst
,
545 if (data
->rh_alarm
.high_alarm
) {
546 *val
= (measurement
> max_hyst
) ? 1 : 0;
547 data
->rh_alarm
.high_alarm
= *val
;
555 static int cc2_read(struct device
*dev
, enum hwmon_sensor_types type
, u32 attr
,
556 int channel
, long *val
)
558 struct cc2_data
*data
= dev_get_drvdata(dev
);
561 mutex_lock(&data
->dev_access_lock
);
565 ret
= cc2_measurement(data
, type
, val
);
569 case hwmon_humidity_input
:
570 ret
= cc2_measurement(data
, type
, val
);
572 case hwmon_humidity_min
:
573 ret
= cc2_get_reg_val(data
, CC2_R_ALARM_L_ON
, val
);
575 case hwmon_humidity_min_hyst
:
576 ret
= cc2_get_reg_val(data
, CC2_R_ALARM_L_OFF
, val
);
578 case hwmon_humidity_max
:
579 ret
= cc2_get_reg_val(data
, CC2_R_ALARM_H_ON
, val
);
581 case hwmon_humidity_max_hyst
:
582 ret
= cc2_get_reg_val(data
, CC2_R_ALARM_H_OFF
, val
);
584 case hwmon_humidity_min_alarm
:
585 ret
= cc2_humidity_min_alarm_status(data
, val
);
587 case hwmon_humidity_max_alarm
:
588 ret
= cc2_humidity_max_alarm_status(data
, val
);
598 mutex_unlock(&data
->dev_access_lock
);
603 static int cc2_write(struct device
*dev
, enum hwmon_sensor_types type
, u32 attr
,
604 int channel
, long val
)
606 struct cc2_data
*data
= dev_get_drvdata(dev
);
611 if (type
!= hwmon_humidity
)
614 if (val
< 0 || val
> CC2_RH_MAX
)
617 mutex_lock(&data
->dev_access_lock
);
620 case hwmon_humidity_min
:
621 cmd
= CC2_W_ALARM_L_ON
;
622 arg
= cc2_rh_to_reg(val
);
623 ret
= cc2_write_reg(data
, cmd
, arg
);
626 case hwmon_humidity_min_hyst
:
627 cmd
= CC2_W_ALARM_L_OFF
;
628 arg
= cc2_rh_to_reg(val
);
629 ret
= cc2_write_reg(data
, cmd
, arg
);
632 case hwmon_humidity_max
:
633 cmd
= CC2_W_ALARM_H_ON
;
634 arg
= cc2_rh_to_reg(val
);
635 ret
= cc2_write_reg(data
, cmd
, arg
);
638 case hwmon_humidity_max_hyst
:
639 cmd
= CC2_W_ALARM_H_OFF
;
640 arg
= cc2_rh_to_reg(val
);
641 ret
= cc2_write_reg(data
, cmd
, arg
);
649 mutex_unlock(&data
->dev_access_lock
);
654 static int cc2_request_ready_irq(struct cc2_data
*data
, struct device
*dev
)
658 data
->irq_ready
= fwnode_irq_get_byname(dev_fwnode(dev
), "ready");
659 if (data
->irq_ready
> 0) {
660 init_completion(&data
->complete
);
661 ret
= devm_request_threaded_irq(dev
, data
->irq_ready
, NULL
,
665 dev_name(dev
), data
);
671 static int cc2_request_alarm_irqs(struct cc2_data
*data
, struct device
*dev
)
675 data
->irq_low
= fwnode_irq_get_byname(dev_fwnode(dev
), "low");
676 if (data
->irq_low
> 0) {
677 ret
= devm_request_threaded_irq(dev
, data
->irq_low
, NULL
,
681 dev_name(dev
), data
);
685 data
->rh_alarm
.low_alarm_visible
= true;
688 data
->irq_high
= fwnode_irq_get_byname(dev_fwnode(dev
), "high");
689 if (data
->irq_high
> 0) {
690 ret
= devm_request_threaded_irq(dev
, data
->irq_high
, NULL
,
694 dev_name(dev
), data
);
698 data
->rh_alarm
.high_alarm_visible
= true;
704 static const struct hwmon_channel_info
*cc2_info
[] = {
705 HWMON_CHANNEL_INFO(temp
, HWMON_T_INPUT
),
706 HWMON_CHANNEL_INFO(humidity
, HWMON_H_INPUT
| HWMON_H_MIN
| HWMON_H_MAX
|
707 HWMON_H_MIN_HYST
| HWMON_H_MAX_HYST
|
708 HWMON_H_MIN_ALARM
| HWMON_H_MAX_ALARM
),
712 static const struct hwmon_ops cc2_hwmon_ops
= {
713 .is_visible
= cc2_is_visible
,
718 static const struct hwmon_chip_info cc2_chip_info
= {
719 .ops
= &cc2_hwmon_ops
,
723 static int cc2_probe(struct i2c_client
*client
)
725 struct cc2_data
*data
;
726 struct device
*dev
= &client
->dev
;
729 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
))
732 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
736 i2c_set_clientdata(client
, data
);
738 mutex_init(&data
->dev_access_lock
);
740 data
->client
= client
;
742 data
->regulator
= devm_regulator_get_exclusive(dev
, "vdd");
743 if (IS_ERR(data
->regulator
))
744 return dev_err_probe(dev
, PTR_ERR(data
->regulator
),
745 "Failed to get regulator\n");
747 ret
= cc2_request_ready_irq(data
, dev
);
749 return dev_err_probe(dev
, ret
, "Failed to request ready irq\n");
751 ret
= cc2_request_alarm_irqs(data
, dev
);
753 return dev_err_probe(dev
, ret
, "Failed to request alarm irqs\n");
755 data
->hwmon
= devm_hwmon_device_register_with_info(dev
, client
->name
,
756 data
, &cc2_chip_info
,
758 if (IS_ERR(data
->hwmon
))
759 return dev_err_probe(dev
, PTR_ERR(data
->hwmon
),
760 "Failed to register hwmon device\n");
765 static void cc2_remove(struct i2c_client
*client
)
767 struct cc2_data
*data
= i2c_get_clientdata(client
);
772 static const struct i2c_device_id cc2_id
[] = {
783 MODULE_DEVICE_TABLE(i2c
, cc2_id
);
785 static const struct of_device_id cc2_of_match
[] = {
786 { .compatible
= "amphenol,cc2d23" },
787 { .compatible
= "amphenol,cc2d23s" },
788 { .compatible
= "amphenol,cc2d25" },
789 { .compatible
= "amphenol,cc2d25s" },
790 { .compatible
= "amphenol,cc2d33" },
791 { .compatible
= "amphenol,cc2d33s" },
792 { .compatible
= "amphenol,cc2d35" },
793 { .compatible
= "amphenol,cc2d35s" },
796 MODULE_DEVICE_TABLE(of
, cc2_of_match
);
798 static struct i2c_driver cc2_driver
= {
801 .of_match_table
= cc2_of_match
,
804 .remove
= cc2_remove
,
807 module_i2c_driver(cc2_driver
);
809 MODULE_AUTHOR("Javier Carrasco <javier.carrasco.cruz@gamil.com>");
810 MODULE_DESCRIPTION("Amphenol ChipCap 2 humidity and temperature sensor driver");
811 MODULE_LICENSE("GPL");