1 // SPDX-License-Identifier: GPL-2.0+
3 * Azoteq IQS620A/621/622/624/625 Multi-Function Sensors
5 * Copyright (C) 2019 Jeff LaBundy <jeff@labundy.com>
7 * These devices rely on application-specific register settings and calibration
8 * data developed in and exported from a suite of GUIs offered by the vendor. A
9 * separate tool converts the GUIs' ASCII-based output into a standard firmware
10 * file parsed by the driver.
12 * Link to datasheets and GUIs: https://www.azoteq.com/
14 * Link to conversion tool: https://github.com/jlabundy/iqs62x-h2bin.git
17 #include <linux/completion.h>
18 #include <linux/delay.h>
19 #include <linux/device.h>
20 #include <linux/err.h>
21 #include <linux/firmware.h>
22 #include <linux/i2c.h>
23 #include <linux/interrupt.h>
24 #include <linux/kernel.h>
25 #include <linux/list.h>
26 #include <linux/mfd/core.h>
27 #include <linux/mfd/iqs62x.h>
28 #include <linux/module.h>
29 #include <linux/notifier.h>
30 #include <linux/of_device.h>
31 #include <linux/property.h>
32 #include <linux/regmap.h>
33 #include <linux/slab.h>
34 #include <asm/unaligned.h>
36 #define IQS62X_PROD_NUM 0x00
38 #define IQS62X_SYS_FLAGS 0x10
39 #define IQS62X_SYS_FLAGS_IN_ATI BIT(2)
41 #define IQS620_HALL_FLAGS 0x16
42 #define IQS621_HALL_FLAGS 0x19
43 #define IQS622_HALL_FLAGS IQS621_HALL_FLAGS
45 #define IQS624_INTERVAL_NUM 0x18
46 #define IQS625_INTERVAL_NUM 0x12
48 #define IQS622_PROX_SETTINGS_4 0x48
49 #define IQS620_PROX_SETTINGS_4 0x50
50 #define IQS620_PROX_SETTINGS_4_SAR_EN BIT(7)
52 #define IQS621_ALS_CAL_DIV_LUX 0x82
53 #define IQS621_ALS_CAL_DIV_IR 0x83
55 #define IQS620_TEMP_CAL_MULT 0xC2
56 #define IQS620_TEMP_CAL_DIV 0xC3
57 #define IQS620_TEMP_CAL_OFFS 0xC4
59 #define IQS62X_SYS_SETTINGS 0xD0
60 #define IQS62X_SYS_SETTINGS_SOFT_RESET BIT(7)
61 #define IQS62X_SYS_SETTINGS_ACK_RESET BIT(6)
62 #define IQS62X_SYS_SETTINGS_EVENT_MODE BIT(5)
63 #define IQS62X_SYS_SETTINGS_CLK_DIV BIT(4)
64 #define IQS62X_SYS_SETTINGS_REDO_ATI BIT(1)
66 #define IQS62X_PWR_SETTINGS 0xD2
67 #define IQS62X_PWR_SETTINGS_DIS_AUTO BIT(5)
68 #define IQS62X_PWR_SETTINGS_PWR_MODE_MASK (BIT(4) | BIT(3))
69 #define IQS62X_PWR_SETTINGS_PWR_MODE_HALT (BIT(4) | BIT(3))
70 #define IQS62X_PWR_SETTINGS_PWR_MODE_NORM 0
72 #define IQS62X_OTP_CMD 0xF0
73 #define IQS62X_OTP_CMD_FG3 0x13
74 #define IQS62X_OTP_DATA 0xF1
75 #define IQS62X_MAX_REG 0xFF
77 #define IQS62X_HALL_CAL_MASK GENMASK(3, 0)
79 #define IQS62X_FW_REC_TYPE_INFO 0
80 #define IQS62X_FW_REC_TYPE_PROD 1
81 #define IQS62X_FW_REC_TYPE_HALL 2
82 #define IQS62X_FW_REC_TYPE_MASK 3
83 #define IQS62X_FW_REC_TYPE_DATA 4
85 #define IQS62X_ATI_POLL_SLEEP_US 10000
86 #define IQS62X_ATI_POLL_TIMEOUT_US 500000
87 #define IQS62X_ATI_STABLE_DELAY_MS 150
89 struct iqs62x_fw_rec
{
96 struct iqs62x_fw_blk
{
97 struct list_head list
;
110 static int iqs62x_dev_init(struct iqs62x_core
*iqs62x
)
112 struct iqs62x_fw_blk
*fw_blk
;
117 list_for_each_entry(fw_blk
, &iqs62x
->fw_blk_head
, list
) {
119 ret
= regmap_update_bits(iqs62x
->regmap
, fw_blk
->addr
,
120 fw_blk
->mask
, *fw_blk
->data
);
122 ret
= regmap_raw_write(iqs62x
->regmap
, fw_blk
->addr
,
123 fw_blk
->data
, fw_blk
->len
);
128 switch (iqs62x
->dev_desc
->prod_num
) {
129 case IQS620_PROD_NUM
:
130 case IQS622_PROD_NUM
:
131 ret
= regmap_read(iqs62x
->regmap
,
132 iqs62x
->dev_desc
->prox_settings
, &val
);
136 if (val
& IQS620_PROX_SETTINGS_4_SAR_EN
)
137 iqs62x
->ui_sel
= IQS62X_UI_SAR1
;
141 case IQS621_PROD_NUM
:
142 ret
= regmap_write(iqs62x
->regmap
, IQS620_GLBL_EVENT_MASK
,
143 IQS620_GLBL_EVENT_MASK_PMU
|
144 iqs62x
->dev_desc
->prox_mask
|
145 iqs62x
->dev_desc
->sar_mask
|
146 iqs62x
->dev_desc
->hall_mask
|
147 iqs62x
->dev_desc
->hyst_mask
|
148 iqs62x
->dev_desc
->temp_mask
|
149 iqs62x
->dev_desc
->als_mask
|
150 iqs62x
->dev_desc
->ir_mask
);
156 ret
= regmap_write(iqs62x
->regmap
, IQS624_HALL_UI
,
157 IQS624_HALL_UI_WHL_EVENT
|
158 IQS624_HALL_UI_INT_EVENT
|
159 IQS624_HALL_UI_AUTO_CAL
);
164 * The IQS625 default interval divider is below the minimum
165 * permissible value, and the datasheet mandates that it is
166 * corrected during initialization (unless an updated value
167 * has already been provided by firmware).
169 * To protect against an unacceptably low user-entered value
170 * stored in the firmware, the same check is extended to the
173 ret
= regmap_read(iqs62x
->regmap
, IQS624_INTERVAL_DIV
, &val
);
177 if (val
>= iqs62x
->dev_desc
->interval_div
)
180 ret
= regmap_write(iqs62x
->regmap
, IQS624_INTERVAL_DIV
,
181 iqs62x
->dev_desc
->interval_div
);
186 ret
= regmap_read(iqs62x
->regmap
, IQS62X_SYS_SETTINGS
, &val
);
190 if (val
& IQS62X_SYS_SETTINGS_CLK_DIV
)
191 clk_div
= iqs62x
->dev_desc
->clk_div
;
193 ret
= regmap_write(iqs62x
->regmap
, IQS62X_SYS_SETTINGS
, val
|
194 IQS62X_SYS_SETTINGS_ACK_RESET
|
195 IQS62X_SYS_SETTINGS_EVENT_MODE
|
196 IQS62X_SYS_SETTINGS_REDO_ATI
);
200 ret
= regmap_read_poll_timeout(iqs62x
->regmap
, IQS62X_SYS_FLAGS
, val
,
201 !(val
& IQS62X_SYS_FLAGS_IN_ATI
),
202 IQS62X_ATI_POLL_SLEEP_US
,
203 IQS62X_ATI_POLL_TIMEOUT_US
* clk_div
);
207 msleep(IQS62X_ATI_STABLE_DELAY_MS
* clk_div
);
212 static int iqs62x_firmware_parse(struct iqs62x_core
*iqs62x
,
213 const struct firmware
*fw
)
215 struct i2c_client
*client
= iqs62x
->client
;
216 struct iqs62x_fw_rec
*fw_rec
;
217 struct iqs62x_fw_blk
*fw_blk
;
222 u8 hall_cal_index
= 0;
224 while (pos
< fw
->size
) {
225 if (pos
+ sizeof(*fw_rec
) > fw
->size
) {
229 fw_rec
= (struct iqs62x_fw_rec
*)(fw
->data
+ pos
);
230 pos
+= sizeof(*fw_rec
);
232 if (pos
+ fw_rec
->len
- 1 > fw
->size
) {
236 pos
+= fw_rec
->len
- 1;
238 switch (fw_rec
->type
) {
239 case IQS62X_FW_REC_TYPE_INFO
:
242 case IQS62X_FW_REC_TYPE_PROD
:
243 if (fw_rec
->data
== iqs62x
->dev_desc
->prod_num
)
246 dev_err(&client
->dev
,
247 "Incompatible product number: 0x%02X\n",
252 case IQS62X_FW_REC_TYPE_HALL
:
253 if (!hall_cal_index
) {
254 ret
= regmap_write(iqs62x
->regmap
,
260 ret
= regmap_read(iqs62x
->regmap
,
261 IQS62X_OTP_DATA
, &val
);
265 hall_cal_index
= val
& IQS62X_HALL_CAL_MASK
;
266 if (!hall_cal_index
) {
267 dev_err(&client
->dev
,
268 "Uncalibrated device\n");
274 if (hall_cal_index
> fw_rec
->len
) {
280 data
= &fw_rec
->data
+ hall_cal_index
- 1;
284 case IQS62X_FW_REC_TYPE_MASK
:
285 if (fw_rec
->len
< (sizeof(mask
) + sizeof(*data
))) {
291 data
= &fw_rec
->data
+ sizeof(mask
);
295 case IQS62X_FW_REC_TYPE_DATA
:
297 data
= &fw_rec
->data
;
302 dev_err(&client
->dev
,
303 "Unrecognized record type: 0x%02X\n",
311 fw_blk
= devm_kzalloc(&client
->dev
,
312 struct_size(fw_blk
, data
, len
),
319 fw_blk
->addr
= fw_rec
->addr
;
322 memcpy(fw_blk
->data
, data
, len
);
324 list_add(&fw_blk
->list
, &iqs62x
->fw_blk_head
);
327 release_firmware(fw
);
332 const struct iqs62x_event_desc iqs62x_events
[IQS62X_NUM_EVENTS
] = {
333 [IQS62X_EVENT_PROX_CH0_T
] = {
334 .reg
= IQS62X_EVENT_PROX
,
338 [IQS62X_EVENT_PROX_CH0_P
] = {
339 .reg
= IQS62X_EVENT_PROX
,
343 [IQS62X_EVENT_PROX_CH1_T
] = {
344 .reg
= IQS62X_EVENT_PROX
,
348 [IQS62X_EVENT_PROX_CH1_P
] = {
349 .reg
= IQS62X_EVENT_PROX
,
353 [IQS62X_EVENT_PROX_CH2_T
] = {
354 .reg
= IQS62X_EVENT_PROX
,
358 [IQS62X_EVENT_PROX_CH2_P
] = {
359 .reg
= IQS62X_EVENT_PROX
,
363 [IQS62X_EVENT_HYST_POS_T
] = {
364 .reg
= IQS62X_EVENT_HYST
,
365 .mask
= BIT(6) | BIT(7),
368 [IQS62X_EVENT_HYST_POS_P
] = {
369 .reg
= IQS62X_EVENT_HYST
,
370 .mask
= BIT(5) | BIT(7),
373 [IQS62X_EVENT_HYST_NEG_T
] = {
374 .reg
= IQS62X_EVENT_HYST
,
375 .mask
= BIT(6) | BIT(7),
376 .val
= BIT(6) | BIT(7),
378 [IQS62X_EVENT_HYST_NEG_P
] = {
379 .reg
= IQS62X_EVENT_HYST
,
380 .mask
= BIT(5) | BIT(7),
381 .val
= BIT(5) | BIT(7),
383 [IQS62X_EVENT_SAR1_ACT
] = {
384 .reg
= IQS62X_EVENT_HYST
,
388 [IQS62X_EVENT_SAR1_QRD
] = {
389 .reg
= IQS62X_EVENT_HYST
,
393 [IQS62X_EVENT_SAR1_MOVE
] = {
394 .reg
= IQS62X_EVENT_HYST
,
398 [IQS62X_EVENT_SAR1_HALT
] = {
399 .reg
= IQS62X_EVENT_HYST
,
403 [IQS62X_EVENT_WHEEL_UP
] = {
404 .reg
= IQS62X_EVENT_WHEEL
,
405 .mask
= BIT(7) | BIT(6),
408 [IQS62X_EVENT_WHEEL_DN
] = {
409 .reg
= IQS62X_EVENT_WHEEL
,
410 .mask
= BIT(7) | BIT(6),
411 .val
= BIT(7) | BIT(6),
413 [IQS62X_EVENT_HALL_N_T
] = {
414 .reg
= IQS62X_EVENT_HALL
,
415 .mask
= BIT(2) | BIT(0),
418 [IQS62X_EVENT_HALL_N_P
] = {
419 .reg
= IQS62X_EVENT_HALL
,
420 .mask
= BIT(1) | BIT(0),
423 [IQS62X_EVENT_HALL_S_T
] = {
424 .reg
= IQS62X_EVENT_HALL
,
425 .mask
= BIT(2) | BIT(0),
426 .val
= BIT(2) | BIT(0),
428 [IQS62X_EVENT_HALL_S_P
] = {
429 .reg
= IQS62X_EVENT_HALL
,
430 .mask
= BIT(1) | BIT(0),
431 .val
= BIT(1) | BIT(0),
433 [IQS62X_EVENT_SYS_RESET
] = {
434 .reg
= IQS62X_EVENT_SYS
,
439 EXPORT_SYMBOL_GPL(iqs62x_events
);
441 static irqreturn_t
iqs62x_irq(int irq
, void *context
)
443 struct iqs62x_core
*iqs62x
= context
;
444 struct i2c_client
*client
= iqs62x
->client
;
445 struct iqs62x_event_data event_data
;
446 struct iqs62x_event_desc event_desc
;
447 enum iqs62x_event_reg event_reg
;
448 unsigned long event_flags
= 0;
450 u8 event_map
[IQS62X_EVENT_SIZE
];
453 * The device asserts the RDY output to signal the beginning of a
454 * communication window, which is closed by an I2C stop condition.
455 * As such, all interrupt status is captured in a single read and
456 * broadcast to any interested sub-device drivers.
458 ret
= regmap_raw_read(iqs62x
->regmap
, IQS62X_SYS_FLAGS
, event_map
,
461 dev_err(&client
->dev
, "Failed to read device status: %d\n",
466 for (i
= 0; i
< sizeof(event_map
); i
++) {
467 event_reg
= iqs62x
->dev_desc
->event_regs
[iqs62x
->ui_sel
][i
];
470 case IQS62X_EVENT_UI_LO
:
471 event_data
.ui_data
= get_unaligned_le16(&event_map
[i
]);
475 case IQS62X_EVENT_UI_HI
:
476 case IQS62X_EVENT_NONE
:
479 case IQS62X_EVENT_ALS
:
480 event_data
.als_flags
= event_map
[i
];
483 case IQS62X_EVENT_IR
:
484 event_data
.ir_flags
= event_map
[i
];
487 case IQS62X_EVENT_INTER
:
488 event_data
.interval
= event_map
[i
];
491 case IQS62X_EVENT_HYST
:
492 event_map
[i
] <<= iqs62x
->dev_desc
->hyst_shift
;
496 case IQS62X_EVENT_WHEEL
:
497 case IQS62X_EVENT_HALL
:
498 case IQS62X_EVENT_PROX
:
499 case IQS62X_EVENT_SYS
:
503 for (j
= 0; j
< IQS62X_NUM_EVENTS
; j
++) {
504 event_desc
= iqs62x_events
[j
];
506 if (event_desc
.reg
!= event_reg
)
509 if ((event_map
[i
] & event_desc
.mask
) == event_desc
.val
)
510 event_flags
|= BIT(j
);
515 * The device resets itself in response to the I2C master stalling
516 * communication past a fixed timeout. In this case, all registers
517 * are restored and any interested sub-device drivers are notified.
519 if (event_flags
& BIT(IQS62X_EVENT_SYS_RESET
)) {
520 dev_err(&client
->dev
, "Unexpected device reset\n");
522 ret
= iqs62x_dev_init(iqs62x
);
524 dev_err(&client
->dev
,
525 "Failed to re-initialize device: %d\n", ret
);
530 ret
= blocking_notifier_call_chain(&iqs62x
->nh
, event_flags
,
532 if (ret
& NOTIFY_STOP_MASK
)
536 * Once the communication window is closed, a small delay is added to
537 * ensure the device's RDY output has been deasserted by the time the
538 * interrupt handler returns.
540 usleep_range(50, 100);
545 static void iqs62x_firmware_load(const struct firmware
*fw
, void *context
)
547 struct iqs62x_core
*iqs62x
= context
;
548 struct i2c_client
*client
= iqs62x
->client
;
552 ret
= iqs62x_firmware_parse(iqs62x
, fw
);
554 dev_err(&client
->dev
, "Failed to parse firmware: %d\n",
560 ret
= iqs62x_dev_init(iqs62x
);
562 dev_err(&client
->dev
, "Failed to initialize device: %d\n", ret
);
566 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
567 NULL
, iqs62x_irq
, IRQF_ONESHOT
,
568 client
->name
, iqs62x
);
570 dev_err(&client
->dev
, "Failed to request IRQ: %d\n", ret
);
574 ret
= devm_mfd_add_devices(&client
->dev
, PLATFORM_DEVID_NONE
,
575 iqs62x
->dev_desc
->sub_devs
,
576 iqs62x
->dev_desc
->num_sub_devs
,
579 dev_err(&client
->dev
, "Failed to add sub-devices: %d\n", ret
);
582 complete_all(&iqs62x
->fw_done
);
585 static const struct mfd_cell iqs620at_sub_devs
[] = {
587 .name
= "iqs62x-keys",
588 .of_compatible
= "azoteq,iqs620a-keys",
591 .name
= "iqs620a-pwm",
592 .of_compatible
= "azoteq,iqs620a-pwm",
594 { .name
= "iqs620at-temp", },
597 static const struct mfd_cell iqs620a_sub_devs
[] = {
599 .name
= "iqs62x-keys",
600 .of_compatible
= "azoteq,iqs620a-keys",
603 .name
= "iqs620a-pwm",
604 .of_compatible
= "azoteq,iqs620a-pwm",
608 static const struct mfd_cell iqs621_sub_devs
[] = {
610 .name
= "iqs62x-keys",
611 .of_compatible
= "azoteq,iqs621-keys",
613 { .name
= "iqs621-als", },
616 static const struct mfd_cell iqs622_sub_devs
[] = {
618 .name
= "iqs62x-keys",
619 .of_compatible
= "azoteq,iqs622-keys",
621 { .name
= "iqs621-als", },
624 static const struct mfd_cell iqs624_sub_devs
[] = {
626 .name
= "iqs62x-keys",
627 .of_compatible
= "azoteq,iqs624-keys",
629 { .name
= "iqs624-pos", },
632 static const struct mfd_cell iqs625_sub_devs
[] = {
634 .name
= "iqs62x-keys",
635 .of_compatible
= "azoteq,iqs625-keys",
637 { .name
= "iqs624-pos", },
640 static const u8 iqs620at_cal_regs
[] = {
641 IQS620_TEMP_CAL_MULT
,
643 IQS620_TEMP_CAL_OFFS
,
646 static const u8 iqs621_cal_regs
[] = {
647 IQS621_ALS_CAL_DIV_LUX
,
648 IQS621_ALS_CAL_DIV_IR
,
651 static const enum iqs62x_event_reg iqs620a_event_regs
[][IQS62X_EVENT_SIZE
] = {
653 IQS62X_EVENT_SYS
, /* 0x10 */
655 IQS62X_EVENT_PROX
, /* 0x12 */
656 IQS62X_EVENT_HYST
, /* 0x13 */
659 IQS62X_EVENT_HALL
, /* 0x16 */
665 IQS62X_EVENT_SYS
, /* 0x10 */
668 IQS62X_EVENT_HYST
, /* 0x13 */
671 IQS62X_EVENT_HALL
, /* 0x16 */
678 static const enum iqs62x_event_reg iqs621_event_regs
[][IQS62X_EVENT_SIZE
] = {
680 IQS62X_EVENT_SYS
, /* 0x10 */
682 IQS62X_EVENT_PROX
, /* 0x12 */
683 IQS62X_EVENT_HYST
, /* 0x13 */
686 IQS62X_EVENT_ALS
, /* 0x16 */
687 IQS62X_EVENT_UI_LO
, /* 0x17 */
688 IQS62X_EVENT_UI_HI
, /* 0x18 */
689 IQS62X_EVENT_HALL
, /* 0x19 */
693 static const enum iqs62x_event_reg iqs622_event_regs
[][IQS62X_EVENT_SIZE
] = {
695 IQS62X_EVENT_SYS
, /* 0x10 */
697 IQS62X_EVENT_PROX
, /* 0x12 */
699 IQS62X_EVENT_ALS
, /* 0x14 */
701 IQS62X_EVENT_IR
, /* 0x16 */
702 IQS62X_EVENT_UI_LO
, /* 0x17 */
703 IQS62X_EVENT_UI_HI
, /* 0x18 */
704 IQS62X_EVENT_HALL
, /* 0x19 */
707 IQS62X_EVENT_SYS
, /* 0x10 */
710 IQS62X_EVENT_HYST
, /* 0x13 */
711 IQS62X_EVENT_ALS
, /* 0x14 */
713 IQS62X_EVENT_IR
, /* 0x16 */
714 IQS62X_EVENT_UI_LO
, /* 0x17 */
715 IQS62X_EVENT_UI_HI
, /* 0x18 */
716 IQS62X_EVENT_HALL
, /* 0x19 */
720 static const enum iqs62x_event_reg iqs624_event_regs
[][IQS62X_EVENT_SIZE
] = {
722 IQS62X_EVENT_SYS
, /* 0x10 */
724 IQS62X_EVENT_PROX
, /* 0x12 */
726 IQS62X_EVENT_WHEEL
, /* 0x14 */
728 IQS62X_EVENT_UI_LO
, /* 0x16 */
729 IQS62X_EVENT_UI_HI
, /* 0x17 */
730 IQS62X_EVENT_INTER
, /* 0x18 */
735 static const enum iqs62x_event_reg iqs625_event_regs
[][IQS62X_EVENT_SIZE
] = {
737 IQS62X_EVENT_SYS
, /* 0x10 */
738 IQS62X_EVENT_PROX
, /* 0x11 */
739 IQS62X_EVENT_INTER
, /* 0x12 */
750 static const struct iqs62x_dev_desc iqs62x_devs
[] = {
752 .dev_name
= "iqs620at",
753 .sub_devs
= iqs620at_sub_devs
,
754 .num_sub_devs
= ARRAY_SIZE(iqs620at_sub_devs
),
756 .prod_num
= IQS620_PROD_NUM
,
758 .cal_regs
= iqs620at_cal_regs
,
759 .num_cal_regs
= ARRAY_SIZE(iqs620at_cal_regs
),
762 .sar_mask
= BIT(1) | BIT(7),
767 .prox_settings
= IQS620_PROX_SETTINGS_4
,
768 .hall_flags
= IQS620_HALL_FLAGS
,
771 .fw_name
= "iqs620a.bin",
772 .event_regs
= &iqs620a_event_regs
[IQS62X_UI_PROX
],
775 .dev_name
= "iqs620a",
776 .sub_devs
= iqs620a_sub_devs
,
777 .num_sub_devs
= ARRAY_SIZE(iqs620a_sub_devs
),
779 .prod_num
= IQS620_PROD_NUM
,
783 .sar_mask
= BIT(1) | BIT(7),
788 .prox_settings
= IQS620_PROX_SETTINGS_4
,
789 .hall_flags
= IQS620_HALL_FLAGS
,
792 .fw_name
= "iqs620a.bin",
793 .event_regs
= &iqs620a_event_regs
[IQS62X_UI_PROX
],
796 .dev_name
= "iqs621",
797 .sub_devs
= iqs621_sub_devs
,
798 .num_sub_devs
= ARRAY_SIZE(iqs621_sub_devs
),
800 .prod_num
= IQS621_PROD_NUM
,
802 .cal_regs
= iqs621_cal_regs
,
803 .num_cal_regs
= ARRAY_SIZE(iqs621_cal_regs
),
811 .als_flags
= IQS621_ALS_FLAGS
,
812 .hall_flags
= IQS621_HALL_FLAGS
,
816 .fw_name
= "iqs621.bin",
817 .event_regs
= &iqs621_event_regs
[IQS62X_UI_PROX
],
820 .dev_name
= "iqs622",
821 .sub_devs
= iqs622_sub_devs
,
822 .num_sub_devs
= ARRAY_SIZE(iqs622_sub_devs
),
824 .prod_num
= IQS622_PROD_NUM
,
833 .prox_settings
= IQS622_PROX_SETTINGS_4
,
834 .als_flags
= IQS622_ALS_FLAGS
,
835 .hall_flags
= IQS622_HALL_FLAGS
,
838 .fw_name
= "iqs622.bin",
839 .event_regs
= &iqs622_event_regs
[IQS62X_UI_PROX
],
842 .dev_name
= "iqs624",
843 .sub_devs
= iqs624_sub_devs
,
844 .num_sub_devs
= ARRAY_SIZE(iqs624_sub_devs
),
846 .prod_num
= IQS624_PROD_NUM
,
849 .interval
= IQS624_INTERVAL_NUM
,
853 .fw_name
= "iqs624.bin",
854 .event_regs
= &iqs624_event_regs
[IQS62X_UI_PROX
],
857 .dev_name
= "iqs625",
858 .sub_devs
= iqs625_sub_devs
,
859 .num_sub_devs
= ARRAY_SIZE(iqs625_sub_devs
),
861 .prod_num
= IQS625_PROD_NUM
,
864 .interval
= IQS625_INTERVAL_NUM
,
868 .fw_name
= "iqs625.bin",
869 .event_regs
= &iqs625_event_regs
[IQS62X_UI_PROX
],
873 static const struct regmap_config iqs62x_map_config
= {
876 .max_register
= IQS62X_MAX_REG
,
879 static int iqs62x_probe(struct i2c_client
*client
)
881 struct iqs62x_core
*iqs62x
;
882 struct iqs62x_info info
;
886 const char *fw_name
= NULL
;
888 iqs62x
= devm_kzalloc(&client
->dev
, sizeof(*iqs62x
), GFP_KERNEL
);
892 i2c_set_clientdata(client
, iqs62x
);
893 iqs62x
->client
= client
;
895 BLOCKING_INIT_NOTIFIER_HEAD(&iqs62x
->nh
);
896 INIT_LIST_HEAD(&iqs62x
->fw_blk_head
);
897 init_completion(&iqs62x
->fw_done
);
899 iqs62x
->regmap
= devm_regmap_init_i2c(client
, &iqs62x_map_config
);
900 if (IS_ERR(iqs62x
->regmap
)) {
901 ret
= PTR_ERR(iqs62x
->regmap
);
902 dev_err(&client
->dev
, "Failed to initialize register map: %d\n",
907 ret
= regmap_raw_read(iqs62x
->regmap
, IQS62X_PROD_NUM
, &info
,
913 * The following sequence validates the device's product and software
914 * numbers. It then determines if the device is factory-calibrated by
915 * checking for nonzero values in the device's designated calibration
916 * registers (if applicable). Depending on the device, the absence of
917 * calibration data indicates a reduced feature set or invalid device.
919 * For devices given in both calibrated and uncalibrated versions, the
920 * calibrated version (e.g. IQS620AT) appears first in the iqs62x_devs
921 * array. The uncalibrated version (e.g. IQS620A) appears next and has
922 * the same product and software numbers, but no calibration registers
925 for (i
= 0; i
< ARRAY_SIZE(iqs62x_devs
); i
++) {
926 if (info
.prod_num
!= iqs62x_devs
[i
].prod_num
)
929 iqs62x
->dev_desc
= &iqs62x_devs
[i
];
931 if (info
.sw_num
< iqs62x
->dev_desc
->sw_num
)
934 sw_num
= info
.sw_num
;
937 * Read each of the device's designated calibration registers,
938 * if any, and exit from the inner loop early if any are equal
939 * to zero (indicating the device is uncalibrated). This could
940 * be acceptable depending on the device (e.g. IQS620A instead
943 for (j
= 0; j
< iqs62x
->dev_desc
->num_cal_regs
; j
++) {
944 ret
= regmap_read(iqs62x
->regmap
,
945 iqs62x
->dev_desc
->cal_regs
[j
], &val
);
954 * If the number of nonzero values read from the device equals
955 * the number of designated calibration registers (which could
956 * be zero), exit from the outer loop early to signal that the
957 * device's product and software numbers match a known device,
958 * and the device is calibrated (if applicable).
960 if (j
== iqs62x
->dev_desc
->num_cal_regs
)
964 if (!iqs62x
->dev_desc
) {
965 dev_err(&client
->dev
, "Unrecognized product number: 0x%02X\n",
971 dev_err(&client
->dev
, "Unrecognized software number: 0x%02X\n",
976 if (i
== ARRAY_SIZE(iqs62x_devs
)) {
977 dev_err(&client
->dev
, "Uncalibrated device\n");
981 device_property_read_string(&client
->dev
, "firmware-name", &fw_name
);
983 ret
= request_firmware_nowait(THIS_MODULE
, FW_ACTION_HOTPLUG
,
984 fw_name
? : iqs62x
->dev_desc
->fw_name
,
985 &client
->dev
, GFP_KERNEL
, iqs62x
,
986 iqs62x_firmware_load
);
988 dev_err(&client
->dev
, "Failed to request firmware: %d\n", ret
);
993 static int iqs62x_remove(struct i2c_client
*client
)
995 struct iqs62x_core
*iqs62x
= i2c_get_clientdata(client
);
997 wait_for_completion(&iqs62x
->fw_done
);
1002 static int __maybe_unused
iqs62x_suspend(struct device
*dev
)
1004 struct iqs62x_core
*iqs62x
= dev_get_drvdata(dev
);
1007 wait_for_completion(&iqs62x
->fw_done
);
1010 * As per the datasheet, automatic mode switching must be disabled
1011 * before the device is placed in or taken out of halt mode.
1013 ret
= regmap_update_bits(iqs62x
->regmap
, IQS62X_PWR_SETTINGS
,
1014 IQS62X_PWR_SETTINGS_DIS_AUTO
, 0xFF);
1018 return regmap_update_bits(iqs62x
->regmap
, IQS62X_PWR_SETTINGS
,
1019 IQS62X_PWR_SETTINGS_PWR_MODE_MASK
,
1020 IQS62X_PWR_SETTINGS_PWR_MODE_HALT
);
1023 static int __maybe_unused
iqs62x_resume(struct device
*dev
)
1025 struct iqs62x_core
*iqs62x
= dev_get_drvdata(dev
);
1028 ret
= regmap_update_bits(iqs62x
->regmap
, IQS62X_PWR_SETTINGS
,
1029 IQS62X_PWR_SETTINGS_PWR_MODE_MASK
,
1030 IQS62X_PWR_SETTINGS_PWR_MODE_NORM
);
1034 return regmap_update_bits(iqs62x
->regmap
, IQS62X_PWR_SETTINGS
,
1035 IQS62X_PWR_SETTINGS_DIS_AUTO
, 0);
1038 static SIMPLE_DEV_PM_OPS(iqs62x_pm
, iqs62x_suspend
, iqs62x_resume
);
1040 static const struct of_device_id iqs62x_of_match
[] = {
1041 { .compatible
= "azoteq,iqs620a" },
1042 { .compatible
= "azoteq,iqs621" },
1043 { .compatible
= "azoteq,iqs622" },
1044 { .compatible
= "azoteq,iqs624" },
1045 { .compatible
= "azoteq,iqs625" },
1048 MODULE_DEVICE_TABLE(of
, iqs62x_of_match
);
1050 static struct i2c_driver iqs62x_i2c_driver
= {
1053 .of_match_table
= iqs62x_of_match
,
1056 .probe_new
= iqs62x_probe
,
1057 .remove
= iqs62x_remove
,
1059 module_i2c_driver(iqs62x_i2c_driver
);
1061 MODULE_AUTHOR("Jeff LaBundy <jeff@labundy.com>");
1062 MODULE_DESCRIPTION("Azoteq IQS620A/621/622/624/625 Multi-Function Sensors");
1063 MODULE_LICENSE("GPL");