| 1 | |
| 2 | |
| 3 | * cc2.c - Support for the Amphenol ChipCap 2 relative humidity, temperature sensor
|
| 4 | *
|
| 5 | * Part numbers supported:
|
| 6 | * CC2D23, CC2D23S, CC2D25, CC2D25S, CC2D33, CC2D33S, CC2D35, CC2D35S
|
| 7 | *
|
| 8 | * Author: Javier Carrasco <[email protected]>
|
| 9 | *
|
| 10 | * Datasheet and application notes:
|
| 11 | * https://www.amphenol-sensors.com/en/telaire/humidity/527-humidity-sensors/3095-chipcap-2
|
| 12 |
|
| 13 |
|
| 14 | #include <linux/bitfield.h>
|
| 15 | #include <linux/bits.h>
|
| 16 | #include <linux/cleanup.h>
|
| 17 | #include <linux/completion.h>
|
| 18 | #include <linux/delay.h>
|
| 19 | #include <linux/hwmon.h>
|
| 20 | #include <linux/i2c.h>
|
| 21 | #include <linux/interrupt.h>
|
| 22 | #include <linux/irq.h>
|
| 23 | #include <linux/module.h>
|
| 24 | #include <linux/regulator/consumer.h>
|
| 25 | #include <linux/mod_devicetable.h>
|
| 26 | #include <linux/property.h>
|
| 27 |
|
| 28 | #define CC2_START_CM 0xA0
|
| 29 | #define CC2_START_NOM 0x80
|
| 30 | #define CC2_R_ALARM_H_ON 0x18
|
| 31 | #define CC2_R_ALARM_H_OFF 0x19
|
| 32 | #define CC2_R_ALARM_L_ON 0x1A
|
| 33 | #define CC2_R_ALARM_L_OFF 0x1B
|
| 34 | #define CC2_RW_OFFSET 0x40
|
| 35 | #define CC2_W_ALARM_H_ON (CC2_R_ALARM_H_ON + CC2_RW_OFFSET)
|
| 36 | #define CC2_W_ALARM_H_OFF (CC2_R_ALARM_H_OFF + CC2_RW_OFFSET)
|
| 37 | #define CC2_W_ALARM_L_ON (CC2_R_ALARM_L_ON + CC2_RW_OFFSET)
|
| 38 | #define CC2_W_ALARM_L_OFF (CC2_R_ALARM_L_OFF + CC2_RW_OFFSET)
|
| 39 |
|
| 40 | #define CC2_STATUS_FIELD GENMASK(7, 6)
|
| 41 | #define CC2_STATUS_VALID_DATA 0x00
|
| 42 | #define CC2_STATUS_STALE_DATA 0x01
|
| 43 | #define CC2_STATUS_CMD_MODE 0x02
|
| 44 |
|
| 45 | #define CC2_RESPONSE_FIELD GENMASK(1, 0)
|
| 46 | #define CC2_RESPONSE_BUSY 0x00
|
| 47 | #define CC2_RESPONSE_ACK 0x01
|
| 48 | #define CC2_RESPONSE_NACK 0x02
|
| 49 |
|
| 50 | #define CC2_ERR_CORR_EEPROM BIT(2)
|
| 51 | #define CC2_ERR_UNCORR_EEPROM BIT(3)
|
| 52 | #define CC2_ERR_RAM_PARITY BIT(4)
|
| 53 | #define CC2_ERR_CONFIG_LOAD BIT(5)
|
| 54 |
|
| 55 | #define CC2_EEPROM_SIZE 10
|
| 56 | #define CC2_EEPROM_DATA_LEN 3
|
| 57 | #define CC2_MEASUREMENT_DATA_LEN 4
|
| 58 |
|
| 59 | #define CC2_RH_DATA_FIELD GENMASK(13, 0)
|
| 60 |
|
| 61 |
|
| 62 | #define CC2_POWER_CYCLE_MS 80
|
| 63 |
|
| 64 | #define CC2_STARTUP_TO_DATA_MS 55
|
| 65 | #define CC2_RESP_START_CM_US 100
|
| 66 | #define CC2_RESP_EEPROM_R_US 100
|
| 67 | #define CC2_RESP_EEPROM_W_MS 12
|
| 68 | #define CC2_STARTUP_TIME_US 1250
|
| 69 |
|
| 70 | #define CC2_RH_MAX (100 * 1000U)
|
| 71 |
|
| 72 | #define CC2_CM_RETRIES 5
|
| 73 |
|
| 74 | struct cc2_rh_alarm_info {
|
| 75 | bool low_alarm;
|
| 76 | bool high_alarm;
|
| 77 | bool low_alarm_visible;
|
| 78 | bool high_alarm_visible;
|
| 79 | };
|
| 80 |
|
| 81 | struct cc2_data {
|
| 82 | struct cc2_rh_alarm_info rh_alarm;
|
| 83 | struct completion complete;
|
| 84 | struct device *hwmon;
|
| 85 | struct i2c_client *client;
|
| 86 | struct regulator *regulator;
|
| 87 | const char *name;
|
| 88 | const char *label;
|
| 89 | int irq_ready;
|
| 90 | int irq_low;
|
| 91 | int irq_high;
|
| 92 | bool process_irqs;
|
| 93 | };
|
| 94 |
|
| 95 |
|
| 96 | static long cc2_rh_convert(u16 data)
|
| 97 | {
|
| 98 | unsigned long tmp = (data & CC2_RH_DATA_FIELD) * CC2_RH_MAX;
|
| 99 |
|
| 100 | return tmp / ((1 << 14) - 1);
|
| 101 | }
|
| 102 |
|
| 103 |
|
| 104 | static u16 cc2_rh_to_reg(long data)
|
| 105 | {
|
| 106 | return data * ((1 << 14) - 1) / CC2_RH_MAX;
|
| 107 | }
|
| 108 |
|
| 109 |
|
| 110 | static long cc2_temp_convert(u16 data)
|
| 111 | {
|
| 112 | unsigned long tmp = ((data >> 2) * 165 * 1000U) / ((1 << 14) - 1);
|
| 113 |
|
| 114 | return tmp - 40 * 1000U;
|
| 115 | }
|
| 116 |
|
| 117 | static int cc2_enable(struct cc2_data *data)
|
| 118 | {
|
| 119 | int ret;
|
| 120 |
|
| 121 |
|
| 122 | if (regulator_is_enabled(data->regulator))
|
| 123 | return 0;
|
| 124 |
|
| 125 |
|
| 126 | try_wait_for_completion(&data->complete);
|
| 127 |
|
| 128 | ret = regulator_enable(data->regulator);
|
| 129 | if (ret < 0)
|
| 130 | return ret;
|
| 131 |
|
| 132 | usleep_range(CC2_STARTUP_TIME_US, CC2_STARTUP_TIME_US + 125);
|
| 133 |
|
| 134 | data->process_irqs = true;
|
| 135 |
|
| 136 | return 0;
|
| 137 | }
|
| 138 |
|
| 139 | static void cc2_disable(struct cc2_data *data)
|
| 140 | {
|
| 141 | int err;
|
| 142 |
|
| 143 |
|
| 144 | data->process_irqs = false;
|
| 145 |
|
| 146 |
|
| 147 | if (regulator_is_enabled(data->regulator)) {
|
| 148 | err = regulator_disable(data->regulator);
|
| 149 | if (err)
|
| 150 | dev_dbg(&data->client->dev, "Failed to disable device");
|
| 151 | }
|
| 152 | }
|
| 153 |
|
| 154 | static int cc2_cmd_response_diagnostic(struct device *dev, u8 status)
|
| 155 | {
|
| 156 | int resp;
|
| 157 |
|
| 158 | if (FIELD_GET(CC2_STATUS_FIELD, status) != CC2_STATUS_CMD_MODE) {
|
| 159 | dev_dbg(dev, "Command sent out of command window\n");
|
| 160 | return -ETIMEDOUT;
|
| 161 | }
|
| 162 |
|
| 163 | resp = FIELD_GET(CC2_RESPONSE_FIELD, status);
|
| 164 | switch (resp) {
|
| 165 | case CC2_RESPONSE_ACK:
|
| 166 | return 0;
|
| 167 | case CC2_RESPONSE_BUSY:
|
| 168 | return -EBUSY;
|
| 169 | case CC2_RESPONSE_NACK:
|
| 170 | if (resp & CC2_ERR_CORR_EEPROM)
|
| 171 | dev_dbg(dev, "Command failed: corrected EEPROM\n");
|
| 172 | if (resp & CC2_ERR_UNCORR_EEPROM)
|
| 173 | dev_dbg(dev, "Command failed: uncorrected EEPROM\n");
|
| 174 | if (resp & CC2_ERR_RAM_PARITY)
|
| 175 | dev_dbg(dev, "Command failed: RAM parity\n");
|
| 176 | if (resp & CC2_ERR_RAM_PARITY)
|
| 177 | dev_dbg(dev, "Command failed: configuration error\n");
|
| 178 | return -ENODATA;
|
| 179 | default:
|
| 180 | dev_dbg(dev, "Unknown command reply\n");
|
| 181 | return -EINVAL;
|
| 182 | }
|
| 183 | }
|
| 184 |
|
| 185 | static int cc2_read_command_status(struct i2c_client *client)
|
| 186 | {
|
| 187 | u8 status;
|
| 188 | int ret;
|
| 189 |
|
| 190 | ret = i2c_master_recv(client, &status, 1);
|
| 191 | if (ret != 1) {
|
| 192 | ret = ret < 0 ? ret : -EIO;
|
| 193 | return ret;
|
| 194 | }
|
| 195 |
|
| 196 | return cc2_cmd_response_diagnostic(&client->dev, status);
|
| 197 | }
|
| 198 |
|
| 199 | |
| 200 | * The command mode is only accessible after sending the START_CM command in the
|
| 201 | * first 10 ms after power-up. Only in case the command window is missed,
|
| 202 | * CC2_CM_RETRIES retries are attempted before giving up and returning an error.
|
| 203 |
|
| 204 | static int cc2_command_mode_start(struct cc2_data *data)
|
| 205 | {
|
| 206 | unsigned long timeout;
|
| 207 | int i, ret;
|
| 208 |
|
| 209 | for (i = 0; i < CC2_CM_RETRIES; i++) {
|
| 210 | ret = cc2_enable(data);
|
| 211 | if (ret < 0)
|
| 212 | return ret;
|
| 213 |
|
| 214 | ret = i2c_smbus_write_word_data(data->client, CC2_START_CM, 0);
|
| 215 | if (ret < 0)
|
| 216 | return ret;
|
| 217 |
|
| 218 | if (data->irq_ready > 0) {
|
| 219 | timeout = usecs_to_jiffies(2 * CC2_RESP_START_CM_US);
|
| 220 | ret = wait_for_completion_timeout(&data->complete,
|
| 221 | timeout);
|
| 222 | if (!ret)
|
| 223 | return -ETIMEDOUT;
|
| 224 | } else {
|
| 225 | usleep_range(CC2_RESP_START_CM_US,
|
| 226 | 2 * CC2_RESP_START_CM_US);
|
| 227 | }
|
| 228 | ret = cc2_read_command_status(data->client);
|
| 229 | if (ret != -ETIMEDOUT || i == CC2_CM_RETRIES)
|
| 230 | break;
|
| 231 |
|
| 232 |
|
| 233 | cc2_disable(data);
|
| 234 | msleep(CC2_POWER_CYCLE_MS);
|
| 235 | }
|
| 236 |
|
| 237 | return ret;
|
| 238 | }
|
| 239 |
|
| 240 | |
| 241 | * reply and the device enters normal operation mode
|
| 242 |
|
| 243 | static int cc2_command_mode_finish(struct cc2_data *data)
|
| 244 | {
|
| 245 | int ret;
|
| 246 |
|
| 247 | ret = i2c_smbus_write_word_data(data->client, CC2_START_NOM, 0);
|
| 248 | if (ret < 0)
|
| 249 | return ret;
|
| 250 |
|
| 251 | return 0;
|
| 252 | }
|
| 253 |
|
| 254 | static int cc2_write_reg(struct cc2_data *data, u8 reg, u16 val)
|
| 255 | {
|
| 256 | unsigned long timeout;
|
| 257 | int ret;
|
| 258 |
|
| 259 | ret = cc2_command_mode_start(data);
|
| 260 | if (ret < 0)
|
| 261 | goto disable;
|
| 262 |
|
| 263 | cpu_to_be16s(&val);
|
| 264 | ret = i2c_smbus_write_word_data(data->client, reg, val);
|
| 265 | if (ret < 0)
|
| 266 | goto disable;
|
| 267 |
|
| 268 | if (data->irq_ready > 0) {
|
| 269 | timeout = msecs_to_jiffies(2 * CC2_RESP_EEPROM_W_MS);
|
| 270 | ret = wait_for_completion_timeout(&data->complete, timeout);
|
| 271 | if (!ret) {
|
| 272 | ret = -ETIMEDOUT;
|
| 273 | goto disable;
|
| 274 | }
|
| 275 | } else {
|
| 276 | msleep(CC2_RESP_EEPROM_W_MS);
|
| 277 | }
|
| 278 |
|
| 279 | ret = cc2_read_command_status(data->client);
|
| 280 |
|
| 281 | disable:
|
| 282 | cc2_disable(data);
|
| 283 |
|
| 284 | return ret;
|
| 285 | }
|
| 286 |
|
| 287 | static int cc2_read_reg(struct cc2_data *data, u8 reg, u16 *val)
|
| 288 | {
|
| 289 | u8 buf[CC2_EEPROM_DATA_LEN];
|
| 290 | unsigned long timeout;
|
| 291 | int ret;
|
| 292 |
|
| 293 | ret = cc2_command_mode_start(data);
|
| 294 | if (ret < 0)
|
| 295 | return ret;
|
| 296 |
|
| 297 | ret = i2c_smbus_write_word_data(data->client, reg, 0);
|
| 298 | if (ret < 0)
|
| 299 | return ret;
|
| 300 |
|
| 301 | if (data->irq_ready > 0) {
|
| 302 | timeout = usecs_to_jiffies(2 * CC2_RESP_EEPROM_R_US);
|
| 303 | ret = wait_for_completion_timeout(&data->complete, timeout);
|
| 304 | if (!ret)
|
| 305 | return -ETIMEDOUT;
|
| 306 |
|
| 307 | } else {
|
| 308 | usleep_range(CC2_RESP_EEPROM_R_US, CC2_RESP_EEPROM_R_US + 10);
|
| 309 | }
|
| 310 | ret = i2c_master_recv(data->client, buf, CC2_EEPROM_DATA_LEN);
|
| 311 | if (ret != CC2_EEPROM_DATA_LEN)
|
| 312 | return ret < 0 ? ret : -EIO;
|
| 313 |
|
| 314 | *val = be16_to_cpup((__be16 *)&buf[1]);
|
| 315 |
|
| 316 | return cc2_read_command_status(data->client);
|
| 317 | }
|
| 318 |
|
| 319 | static int cc2_get_reg_val(struct cc2_data *data, u8 reg, long *val)
|
| 320 | {
|
| 321 | u16 reg_val;
|
| 322 | int ret;
|
| 323 |
|
| 324 | ret = cc2_read_reg(data, reg, ®_val);
|
| 325 | if (!ret)
|
| 326 | *val = cc2_rh_convert(reg_val);
|
| 327 |
|
| 328 | cc2_disable(data);
|
| 329 |
|
| 330 | return ret;
|
| 331 | }
|
| 332 |
|
| 333 | static int cc2_data_fetch(struct i2c_client *client,
|
| 334 | enum hwmon_sensor_types type, long *val)
|
| 335 | {
|
| 336 | u8 data[CC2_MEASUREMENT_DATA_LEN];
|
| 337 | u8 status;
|
| 338 | int ret;
|
| 339 |
|
| 340 | ret = i2c_master_recv(client, data, CC2_MEASUREMENT_DATA_LEN);
|
| 341 | if (ret != CC2_MEASUREMENT_DATA_LEN) {
|
| 342 | ret = ret < 0 ? ret : -EIO;
|
| 343 | return ret;
|
| 344 | }
|
| 345 | status = FIELD_GET(CC2_STATUS_FIELD, data[0]);
|
| 346 | if (status == CC2_STATUS_STALE_DATA)
|
| 347 | return -EBUSY;
|
| 348 |
|
| 349 | if (status != CC2_STATUS_VALID_DATA)
|
| 350 | return -EIO;
|
| 351 |
|
| 352 | switch (type) {
|
| 353 | case hwmon_humidity:
|
| 354 | *val = cc2_rh_convert(be16_to_cpup((__be16 *)&data[0]));
|
| 355 | break;
|
| 356 | case hwmon_temp:
|
| 357 | *val = cc2_temp_convert(be16_to_cpup((__be16 *)&data[2]));
|
| 358 | break;
|
| 359 | default:
|
| 360 | return -EINVAL;
|
| 361 | }
|
| 362 |
|
| 363 | return 0;
|
| 364 | }
|
| 365 |
|
| 366 | static int cc2_read_measurement(struct cc2_data *data,
|
| 367 | enum hwmon_sensor_types type, long *val)
|
| 368 | {
|
| 369 | unsigned long timeout;
|
| 370 | int ret;
|
| 371 |
|
| 372 | if (data->irq_ready > 0) {
|
| 373 | timeout = msecs_to_jiffies(CC2_STARTUP_TO_DATA_MS * 2);
|
| 374 | ret = wait_for_completion_timeout(&data->complete, timeout);
|
| 375 | if (!ret)
|
| 376 | return -ETIMEDOUT;
|
| 377 |
|
| 378 | } else {
|
| 379 | msleep(CC2_STARTUP_TO_DATA_MS);
|
| 380 | }
|
| 381 |
|
| 382 | ret = cc2_data_fetch(data->client, type, val);
|
| 383 |
|
| 384 | return ret;
|
| 385 | }
|
| 386 |
|
| 387 | |
| 388 | * A measurement requires enabling the device, waiting for the automatic
|
| 389 | * measurement to finish, reading the measurement data and disabling the device
|
| 390 | * again.
|
| 391 |
|
| 392 | static int cc2_measurement(struct cc2_data *data, enum hwmon_sensor_types type,
|
| 393 | long *val)
|
| 394 | {
|
| 395 | int ret;
|
| 396 |
|
| 397 | ret = cc2_enable(data);
|
| 398 | if (ret)
|
| 399 | return ret;
|
| 400 |
|
| 401 | ret = cc2_read_measurement(data, type, val);
|
| 402 |
|
| 403 | cc2_disable(data);
|
| 404 |
|
| 405 | return ret;
|
| 406 | }
|
| 407 |
|
| 408 | |
| 409 | * In order to check alarm status, the corresponding ALARM_OFF (hysteresis)
|
| 410 | * register must be read and a new measurement must be carried out to trigger
|
| 411 | * the alarm signals. Given that the device carries out a measurement after
|
| 412 | * exiting the command mode, there is no need to force two power-up sequences.
|
| 413 | * Instead, a NOM command is sent and the device is disabled after the
|
| 414 | * measurement is read.
|
| 415 |
|
| 416 | static int cc2_read_hyst_and_measure(struct cc2_data *data, u8 reg,
|
| 417 | long *hyst, long *measurement)
|
| 418 | {
|
| 419 | u16 reg_val;
|
| 420 | int ret;
|
| 421 |
|
| 422 | ret = cc2_read_reg(data, reg, ®_val);
|
| 423 | if (ret)
|
| 424 | goto disable;
|
| 425 |
|
| 426 | *hyst = cc2_rh_convert(reg_val);
|
| 427 |
|
| 428 | ret = cc2_command_mode_finish(data);
|
| 429 | if (ret)
|
| 430 | goto disable;
|
| 431 |
|
| 432 | ret = cc2_read_measurement(data, hwmon_humidity, measurement);
|
| 433 |
|
| 434 | disable:
|
| 435 | cc2_disable(data);
|
| 436 |
|
| 437 | return ret;
|
| 438 | }
|
| 439 |
|
| 440 | static umode_t cc2_is_visible(const void *data, enum hwmon_sensor_types type,
|
| 441 | u32 attr, int channel)
|
| 442 | {
|
| 443 | const struct cc2_data *cc2 = data;
|
| 444 |
|
| 445 | switch (type) {
|
| 446 | case hwmon_humidity:
|
| 447 | switch (attr) {
|
| 448 | case hwmon_humidity_input:
|
| 449 | return 0444;
|
| 450 | case hwmon_humidity_label:
|
| 451 | return cc2->label ? 0444 : 0;
|
| 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;
|
| 462 | default:
|
| 463 | return 0;
|
| 464 | }
|
| 465 | case hwmon_temp:
|
| 466 | switch (attr) {
|
| 467 | case hwmon_temp_input:
|
| 468 | return 0444;
|
| 469 | case hwmon_temp_label:
|
| 470 | return cc2->label ? 0444 : 0;
|
| 471 | default:
|
| 472 | return 0;
|
| 473 | }
|
| 474 | default:
|
| 475 | break;
|
| 476 | }
|
| 477 |
|
| 478 | return 0;
|
| 479 | }
|
| 480 |
|
| 481 | static irqreturn_t cc2_ready_interrupt(int irq, void *data)
|
| 482 | {
|
| 483 | struct cc2_data *cc2 = data;
|
| 484 |
|
| 485 | if (cc2->process_irqs)
|
| 486 | complete(&cc2->complete);
|
| 487 |
|
| 488 | return IRQ_HANDLED;
|
| 489 | }
|
| 490 |
|
| 491 | static irqreturn_t cc2_low_interrupt(int irq, void *data)
|
| 492 | {
|
| 493 | struct cc2_data *cc2 = data;
|
| 494 |
|
| 495 | if (cc2->process_irqs) {
|
| 496 | hwmon_notify_event(cc2->hwmon, hwmon_humidity,
|
| 497 | hwmon_humidity_min_alarm, 0);
|
| 498 | cc2->rh_alarm.low_alarm = true;
|
| 499 | }
|
| 500 |
|
| 501 | return IRQ_HANDLED;
|
| 502 | }
|
| 503 |
|
| 504 | static irqreturn_t cc2_high_interrupt(int irq, void *data)
|
| 505 | {
|
| 506 | struct cc2_data *cc2 = data;
|
| 507 |
|
| 508 | if (cc2->process_irqs) {
|
| 509 | hwmon_notify_event(cc2->hwmon, hwmon_humidity,
|
| 510 | hwmon_humidity_max_alarm, 0);
|
| 511 | cc2->rh_alarm.high_alarm = true;
|
| 512 | }
|
| 513 |
|
| 514 | return IRQ_HANDLED;
|
| 515 | }
|
| 516 |
|
| 517 | static int cc2_humidity_min_alarm_status(struct cc2_data *data, long *val)
|
| 518 | {
|
| 519 | long measurement, min_hyst;
|
| 520 | int ret;
|
| 521 |
|
| 522 | ret = cc2_read_hyst_and_measure(data, CC2_R_ALARM_L_OFF, &min_hyst,
|
| 523 | &measurement);
|
| 524 | if (ret < 0)
|
| 525 | return ret;
|
| 526 |
|
| 527 | if (data->rh_alarm.low_alarm) {
|
| 528 | *val = (measurement < min_hyst) ? 1 : 0;
|
| 529 | data->rh_alarm.low_alarm = *val;
|
| 530 | } else {
|
| 531 | *val = 0;
|
| 532 | }
|
| 533 |
|
| 534 | return 0;
|
| 535 | }
|
| 536 |
|
| 537 | static int cc2_humidity_max_alarm_status(struct cc2_data *data, long *val)
|
| 538 | {
|
| 539 | long measurement, max_hyst;
|
| 540 | int ret;
|
| 541 |
|
| 542 | ret = cc2_read_hyst_and_measure(data, CC2_R_ALARM_H_OFF, &max_hyst,
|
| 543 | &measurement);
|
| 544 | if (ret < 0)
|
| 545 | return ret;
|
| 546 |
|
| 547 | if (data->rh_alarm.high_alarm) {
|
| 548 | *val = (measurement > max_hyst) ? 1 : 0;
|
| 549 | data->rh_alarm.high_alarm = *val;
|
| 550 | } else {
|
| 551 | *val = 0;
|
| 552 | }
|
| 553 |
|
| 554 | return 0;
|
| 555 | }
|
| 556 |
|
| 557 | static int cc2_read_string(struct device *dev, enum hwmon_sensor_types type,
|
| 558 | u32 attr, int channel, const char **str)
|
| 559 | {
|
| 560 | struct cc2_data *data = dev_get_drvdata(dev);
|
| 561 |
|
| 562 | *str = data->label;
|
| 563 |
|
| 564 | return 0;
|
| 565 | }
|
| 566 |
|
| 567 | static int cc2_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
|
| 568 | int channel, long *val)
|
| 569 | {
|
| 570 | struct cc2_data *data = dev_get_drvdata(dev);
|
| 571 |
|
| 572 | switch (type) {
|
| 573 | case hwmon_temp:
|
| 574 | return cc2_measurement(data, type, val);
|
| 575 | case hwmon_humidity:
|
| 576 | switch (attr) {
|
| 577 | case hwmon_humidity_input:
|
| 578 | return cc2_measurement(data, type, val);
|
| 579 | case hwmon_humidity_min:
|
| 580 | return cc2_get_reg_val(data, CC2_R_ALARM_L_ON, val);
|
| 581 | case hwmon_humidity_min_hyst:
|
| 582 | return cc2_get_reg_val(data, CC2_R_ALARM_L_OFF, val);
|
| 583 | case hwmon_humidity_max:
|
| 584 | return cc2_get_reg_val(data, CC2_R_ALARM_H_ON, val);
|
| 585 | case hwmon_humidity_max_hyst:
|
| 586 | return cc2_get_reg_val(data, CC2_R_ALARM_H_OFF, val);
|
| 587 | case hwmon_humidity_min_alarm:
|
| 588 | return cc2_humidity_min_alarm_status(data, val);
|
| 589 | case hwmon_humidity_max_alarm:
|
| 590 | return cc2_humidity_max_alarm_status(data, val);
|
| 591 | default:
|
| 592 | return -EOPNOTSUPP;
|
| 593 | }
|
| 594 | default:
|
| 595 | return -EOPNOTSUPP;
|
| 596 | }
|
| 597 | }
|
| 598 |
|
| 599 | static int cc2_write(struct device *dev, enum hwmon_sensor_types type, u32 attr,
|
| 600 | int channel, long val)
|
| 601 | {
|
| 602 | struct cc2_data *data = dev_get_drvdata(dev);
|
| 603 | u16 arg;
|
| 604 | u8 cmd;
|
| 605 |
|
| 606 | if (type != hwmon_humidity)
|
| 607 | return -EOPNOTSUPP;
|
| 608 |
|
| 609 | if (val < 0 || val > CC2_RH_MAX)
|
| 610 | return -EINVAL;
|
| 611 |
|
| 612 | switch (attr) {
|
| 613 | case hwmon_humidity_min:
|
| 614 | cmd = CC2_W_ALARM_L_ON;
|
| 615 | arg = cc2_rh_to_reg(val);
|
| 616 | return cc2_write_reg(data, cmd, arg);
|
| 617 | case hwmon_humidity_min_hyst:
|
| 618 | cmd = CC2_W_ALARM_L_OFF;
|
| 619 | arg = cc2_rh_to_reg(val);
|
| 620 | return cc2_write_reg(data, cmd, arg);
|
| 621 | case hwmon_humidity_max:
|
| 622 | cmd = CC2_W_ALARM_H_ON;
|
| 623 | arg = cc2_rh_to_reg(val);
|
| 624 | return cc2_write_reg(data, cmd, arg);
|
| 625 | case hwmon_humidity_max_hyst:
|
| 626 | cmd = CC2_W_ALARM_H_OFF;
|
| 627 | arg = cc2_rh_to_reg(val);
|
| 628 | return cc2_write_reg(data, cmd, arg);
|
| 629 | default:
|
| 630 | return -EOPNOTSUPP;
|
| 631 | }
|
| 632 | }
|
| 633 |
|
| 634 | static int cc2_request_ready_irq(struct cc2_data *data, struct device *dev)
|
| 635 | {
|
| 636 | int ret = 0;
|
| 637 |
|
| 638 | data->irq_ready = fwnode_irq_get_byname(dev_fwnode(dev), "ready");
|
| 639 | if (data->irq_ready > 0) {
|
| 640 | init_completion(&data->complete);
|
| 641 | ret = devm_request_threaded_irq(dev, data->irq_ready, NULL,
|
| 642 | cc2_ready_interrupt,
|
| 643 | IRQF_ONESHOT |
|
| 644 | IRQF_TRIGGER_RISING,
|
| 645 | dev_name(dev), data);
|
| 646 | }
|
| 647 |
|
| 648 | return ret;
|
| 649 | }
|
| 650 |
|
| 651 | static int cc2_request_alarm_irqs(struct cc2_data *data, struct device *dev)
|
| 652 | {
|
| 653 | int ret = 0;
|
| 654 |
|
| 655 | data->irq_low = fwnode_irq_get_byname(dev_fwnode(dev), "low");
|
| 656 | if (data->irq_low > 0) {
|
| 657 | ret = devm_request_threaded_irq(dev, data->irq_low, NULL,
|
| 658 | cc2_low_interrupt,
|
| 659 | IRQF_ONESHOT |
|
| 660 | IRQF_TRIGGER_RISING,
|
| 661 | dev_name(dev), data);
|
| 662 | if (ret)
|
| 663 | return ret;
|
| 664 |
|
| 665 | data->rh_alarm.low_alarm_visible = true;
|
| 666 | }
|
| 667 |
|
| 668 | data->irq_high = fwnode_irq_get_byname(dev_fwnode(dev), "high");
|
| 669 | if (data->irq_high > 0) {
|
| 670 | ret = devm_request_threaded_irq(dev, data->irq_high, NULL,
|
| 671 | cc2_high_interrupt,
|
| 672 | IRQF_ONESHOT |
|
| 673 | IRQF_TRIGGER_RISING,
|
| 674 | dev_name(dev), data);
|
| 675 | if (ret)
|
| 676 | return ret;
|
| 677 |
|
| 678 | data->rh_alarm.high_alarm_visible = true;
|
| 679 | }
|
| 680 |
|
| 681 | return ret;
|
| 682 | }
|
| 683 |
|
| 684 | static const struct hwmon_channel_info *cc2_info[] = {
|
| 685 | HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT | HWMON_T_LABEL),
|
| 686 | HWMON_CHANNEL_INFO(humidity, HWMON_H_INPUT | HWMON_H_LABEL |
|
| 687 | HWMON_H_MIN | HWMON_H_MAX |
|
| 688 | HWMON_H_MIN_HYST | HWMON_H_MAX_HYST |
|
| 689 | HWMON_H_MIN_ALARM | HWMON_H_MAX_ALARM),
|
| 690 | NULL
|
| 691 | };
|
| 692 |
|
| 693 | static const struct hwmon_ops cc2_hwmon_ops = {
|
| 694 | .is_visible = cc2_is_visible,
|
| 695 | .read = cc2_read,
|
| 696 | .read_string = cc2_read_string,
|
| 697 | .write = cc2_write,
|
| 698 | };
|
| 699 |
|
| 700 | static const struct hwmon_chip_info cc2_chip_info = {
|
| 701 | .ops = &cc2_hwmon_ops,
|
| 702 | .info = cc2_info,
|
| 703 | };
|
| 704 |
|
| 705 | static int cc2_probe(struct i2c_client *client)
|
| 706 | {
|
| 707 | struct cc2_data *data;
|
| 708 | struct device *dev = &client->dev;
|
| 709 | int ret;
|
| 710 |
|
| 711 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
|
| 712 | return -EOPNOTSUPP;
|
| 713 |
|
| 714 | data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
|
| 715 | if (!data)
|
| 716 | return -ENOMEM;
|
| 717 |
|
| 718 | i2c_set_clientdata(client, data);
|
| 719 |
|
| 720 | data->client = client;
|
| 721 |
|
| 722 | data->regulator = devm_regulator_get_exclusive(dev, "vdd");
|
| 723 | if (IS_ERR(data->regulator))
|
| 724 | return dev_err_probe(dev, PTR_ERR(data->regulator),
|
| 725 | "Failed to get regulator\n");
|
| 726 |
|
| 727 | device_property_read_string(dev, "label", &data->label);
|
| 728 |
|
| 729 | ret = cc2_request_ready_irq(data, dev);
|
| 730 | if (ret)
|
| 731 | return dev_err_probe(dev, ret, "Failed to request ready irq\n");
|
| 732 |
|
| 733 | ret = cc2_request_alarm_irqs(data, dev);
|
| 734 | if (ret)
|
| 735 | return dev_err_probe(dev, ret, "Failed to request alarm irqs\n");
|
| 736 |
|
| 737 | data->hwmon = devm_hwmon_device_register_with_info(dev, client->name,
|
| 738 | data, &cc2_chip_info,
|
| 739 | NULL);
|
| 740 | if (IS_ERR(data->hwmon))
|
| 741 | return dev_err_probe(dev, PTR_ERR(data->hwmon),
|
| 742 | "Failed to register hwmon device\n");
|
| 743 |
|
| 744 | return 0;
|
| 745 | }
|
| 746 |
|
| 747 | static void cc2_remove(struct i2c_client *client)
|
| 748 | {
|
| 749 | struct cc2_data *data = i2c_get_clientdata(client);
|
| 750 |
|
| 751 | cc2_disable(data);
|
| 752 | }
|
| 753 |
|
| 754 | static const struct i2c_device_id cc2_id[] = {
|
| 755 | { .name = "cc2d23" },
|
| 756 | { .name = "cc2d23s" },
|
| 757 | { .name = "cc2d25" },
|
| 758 | { .name = "cc2d25s" },
|
| 759 | { .name = "cc2d33" },
|
| 760 | { .name = "cc2d33s" },
|
| 761 | { .name = "cc2d35" },
|
| 762 | { .name = "cc2d35s" },
|
| 763 | { }
|
| 764 | };
|
| 765 | MODULE_DEVICE_TABLE(i2c, cc2_id);
|
| 766 |
|
| 767 | static const struct of_device_id cc2_of_match[] = {
|
| 768 | { .compatible = "amphenol,cc2d23" },
|
| 769 | { .compatible = "amphenol,cc2d23s" },
|
| 770 | { .compatible = "amphenol,cc2d25" },
|
| 771 | { .compatible = "amphenol,cc2d25s" },
|
| 772 | { .compatible = "amphenol,cc2d33" },
|
| 773 | { .compatible = "amphenol,cc2d33s" },
|
| 774 | { .compatible = "amphenol,cc2d35" },
|
| 775 | { .compatible = "amphenol,cc2d35s" },
|
| 776 | { },
|
| 777 | };
|
| 778 | MODULE_DEVICE_TABLE(of, cc2_of_match);
|
| 779 |
|
| 780 | static struct i2c_driver cc2_driver = {
|
| 781 | .driver = {
|
| 782 | .name = "cc2d23",
|
| 783 | .of_match_table = cc2_of_match,
|
| 784 | },
|
| 785 | .probe = cc2_probe,
|
| 786 | .remove = cc2_remove,
|
| 787 | .id_table = cc2_id,
|
| 788 | };
|
| 789 | module_i2c_driver(cc2_driver);
|
| 790 |
|
| 791 | MODULE_AUTHOR("Javier Carrasco <[email protected]>");
|
| 792 | MODULE_DESCRIPTION("Amphenol ChipCap 2 humidity and temperature sensor driver");
|
| 793 | MODULE_LICENSE("GPL");
|