0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/device.h>
0009 #include <linux/input.h>
0010 #include <linux/kernel.h>
0011 #include <linux/mfd/iqs62x.h>
0012 #include <linux/module.h>
0013 #include <linux/notifier.h>
0014 #include <linux/platform_device.h>
0015 #include <linux/property.h>
0016 #include <linux/regmap.h>
0017 #include <linux/slab.h>
0018
0019 enum {
0020 IQS62X_SW_HALL_N,
0021 IQS62X_SW_HALL_S,
0022 };
0023
0024 static const char * const iqs62x_switch_names[] = {
0025 [IQS62X_SW_HALL_N] = "hall-switch-north",
0026 [IQS62X_SW_HALL_S] = "hall-switch-south",
0027 };
0028
0029 struct iqs62x_switch_desc {
0030 enum iqs62x_event_flag flag;
0031 unsigned int code;
0032 bool enabled;
0033 };
0034
0035 struct iqs62x_keys_private {
0036 struct iqs62x_core *iqs62x;
0037 struct input_dev *input;
0038 struct notifier_block notifier;
0039 struct iqs62x_switch_desc switches[ARRAY_SIZE(iqs62x_switch_names)];
0040 unsigned int keycode[IQS62X_NUM_KEYS];
0041 unsigned int keycodemax;
0042 u8 interval;
0043 };
0044
0045 static int iqs62x_keys_parse_prop(struct platform_device *pdev,
0046 struct iqs62x_keys_private *iqs62x_keys)
0047 {
0048 struct fwnode_handle *child;
0049 unsigned int val;
0050 int ret, i;
0051
0052 ret = device_property_count_u32(&pdev->dev, "linux,keycodes");
0053 if (ret > IQS62X_NUM_KEYS) {
0054 dev_err(&pdev->dev, "Too many keycodes present\n");
0055 return -EINVAL;
0056 } else if (ret < 0) {
0057 dev_err(&pdev->dev, "Failed to count keycodes: %d\n", ret);
0058 return ret;
0059 }
0060 iqs62x_keys->keycodemax = ret;
0061
0062 ret = device_property_read_u32_array(&pdev->dev, "linux,keycodes",
0063 iqs62x_keys->keycode,
0064 iqs62x_keys->keycodemax);
0065 if (ret) {
0066 dev_err(&pdev->dev, "Failed to read keycodes: %d\n", ret);
0067 return ret;
0068 }
0069
0070 for (i = 0; i < ARRAY_SIZE(iqs62x_keys->switches); i++) {
0071 child = device_get_named_child_node(&pdev->dev,
0072 iqs62x_switch_names[i]);
0073 if (!child)
0074 continue;
0075
0076 ret = fwnode_property_read_u32(child, "linux,code", &val);
0077 if (ret) {
0078 dev_err(&pdev->dev, "Failed to read switch code: %d\n",
0079 ret);
0080 fwnode_handle_put(child);
0081 return ret;
0082 }
0083 iqs62x_keys->switches[i].code = val;
0084 iqs62x_keys->switches[i].enabled = true;
0085
0086 if (fwnode_property_present(child, "azoteq,use-prox"))
0087 iqs62x_keys->switches[i].flag = (i == IQS62X_SW_HALL_N ?
0088 IQS62X_EVENT_HALL_N_P :
0089 IQS62X_EVENT_HALL_S_P);
0090 else
0091 iqs62x_keys->switches[i].flag = (i == IQS62X_SW_HALL_N ?
0092 IQS62X_EVENT_HALL_N_T :
0093 IQS62X_EVENT_HALL_S_T);
0094
0095 fwnode_handle_put(child);
0096 }
0097
0098 return 0;
0099 }
0100
0101 static int iqs62x_keys_init(struct iqs62x_keys_private *iqs62x_keys)
0102 {
0103 struct iqs62x_core *iqs62x = iqs62x_keys->iqs62x;
0104 enum iqs62x_event_flag flag;
0105 unsigned int event_reg, val;
0106 unsigned int event_mask = 0;
0107 int ret, i;
0108
0109 switch (iqs62x->dev_desc->prod_num) {
0110 case IQS620_PROD_NUM:
0111 case IQS621_PROD_NUM:
0112 case IQS622_PROD_NUM:
0113 event_reg = IQS620_GLBL_EVENT_MASK;
0114
0115
0116
0117
0118
0119 for (i = 0; i < iqs62x_keys->keycodemax; i++) {
0120 if (iqs62x_keys->keycode[i] == KEY_RESERVED)
0121 continue;
0122
0123 if (iqs62x_events[i].reg == IQS62X_EVENT_PROX)
0124 event_mask |= iqs62x->dev_desc->prox_mask;
0125 else if (iqs62x_events[i].reg == IQS62X_EVENT_HYST)
0126 event_mask |= (iqs62x->dev_desc->hyst_mask |
0127 iqs62x->dev_desc->sar_mask);
0128 }
0129
0130 ret = regmap_read(iqs62x->regmap, iqs62x->dev_desc->hall_flags,
0131 &val);
0132 if (ret)
0133 return ret;
0134
0135
0136
0137
0138
0139 for (i = 0; i < ARRAY_SIZE(iqs62x_keys->switches); i++) {
0140 if (!(iqs62x_keys->switches[i].enabled))
0141 continue;
0142
0143 flag = iqs62x_keys->switches[i].flag;
0144
0145 if (iqs62x_events[flag].reg != IQS62X_EVENT_HALL)
0146 continue;
0147
0148 event_mask |= iqs62x->dev_desc->hall_mask;
0149
0150 input_report_switch(iqs62x_keys->input,
0151 iqs62x_keys->switches[i].code,
0152 (val & iqs62x_events[flag].mask) ==
0153 iqs62x_events[flag].val);
0154 }
0155
0156 input_sync(iqs62x_keys->input);
0157 break;
0158
0159 case IQS624_PROD_NUM:
0160 event_reg = IQS624_HALL_UI;
0161
0162
0163
0164
0165
0166 if (iqs62x_keys->keycode[IQS62X_EVENT_WHEEL_UP] != KEY_RESERVED)
0167 event_mask |= IQS624_HALL_UI_INT_EVENT;
0168
0169 if (iqs62x_keys->keycode[IQS62X_EVENT_WHEEL_DN] != KEY_RESERVED)
0170 event_mask |= IQS624_HALL_UI_INT_EVENT;
0171
0172 ret = regmap_read(iqs62x->regmap, iqs62x->dev_desc->interval,
0173 &val);
0174 if (ret)
0175 return ret;
0176
0177 iqs62x_keys->interval = val;
0178 break;
0179
0180 default:
0181 return 0;
0182 }
0183
0184 return regmap_update_bits(iqs62x->regmap, event_reg, event_mask, 0);
0185 }
0186
0187 static int iqs62x_keys_notifier(struct notifier_block *notifier,
0188 unsigned long event_flags, void *context)
0189 {
0190 struct iqs62x_event_data *event_data = context;
0191 struct iqs62x_keys_private *iqs62x_keys;
0192 int ret, i;
0193
0194 iqs62x_keys = container_of(notifier, struct iqs62x_keys_private,
0195 notifier);
0196
0197 if (event_flags & BIT(IQS62X_EVENT_SYS_RESET)) {
0198 ret = iqs62x_keys_init(iqs62x_keys);
0199 if (ret) {
0200 dev_err(iqs62x_keys->input->dev.parent,
0201 "Failed to re-initialize device: %d\n", ret);
0202 return NOTIFY_BAD;
0203 }
0204
0205 return NOTIFY_OK;
0206 }
0207
0208 for (i = 0; i < iqs62x_keys->keycodemax; i++) {
0209 if (iqs62x_events[i].reg == IQS62X_EVENT_WHEEL &&
0210 event_data->interval == iqs62x_keys->interval)
0211 continue;
0212
0213 input_report_key(iqs62x_keys->input, iqs62x_keys->keycode[i],
0214 event_flags & BIT(i));
0215 }
0216
0217 for (i = 0; i < ARRAY_SIZE(iqs62x_keys->switches); i++)
0218 if (iqs62x_keys->switches[i].enabled)
0219 input_report_switch(iqs62x_keys->input,
0220 iqs62x_keys->switches[i].code,
0221 event_flags &
0222 BIT(iqs62x_keys->switches[i].flag));
0223
0224 input_sync(iqs62x_keys->input);
0225
0226 if (event_data->interval == iqs62x_keys->interval)
0227 return NOTIFY_OK;
0228
0229
0230
0231
0232
0233 if (event_flags & BIT(IQS62X_EVENT_WHEEL_UP)) {
0234 input_report_key(iqs62x_keys->input,
0235 iqs62x_keys->keycode[IQS62X_EVENT_WHEEL_UP],
0236 0);
0237 input_sync(iqs62x_keys->input);
0238 } else if (event_flags & BIT(IQS62X_EVENT_WHEEL_DN)) {
0239 input_report_key(iqs62x_keys->input,
0240 iqs62x_keys->keycode[IQS62X_EVENT_WHEEL_DN],
0241 0);
0242 input_sync(iqs62x_keys->input);
0243 }
0244
0245 iqs62x_keys->interval = event_data->interval;
0246
0247 return NOTIFY_OK;
0248 }
0249
0250 static int iqs62x_keys_probe(struct platform_device *pdev)
0251 {
0252 struct iqs62x_core *iqs62x = dev_get_drvdata(pdev->dev.parent);
0253 struct iqs62x_keys_private *iqs62x_keys;
0254 struct input_dev *input;
0255 int ret, i;
0256
0257 iqs62x_keys = devm_kzalloc(&pdev->dev, sizeof(*iqs62x_keys),
0258 GFP_KERNEL);
0259 if (!iqs62x_keys)
0260 return -ENOMEM;
0261
0262 platform_set_drvdata(pdev, iqs62x_keys);
0263
0264 ret = iqs62x_keys_parse_prop(pdev, iqs62x_keys);
0265 if (ret)
0266 return ret;
0267
0268 input = devm_input_allocate_device(&pdev->dev);
0269 if (!input)
0270 return -ENOMEM;
0271
0272 input->keycodemax = iqs62x_keys->keycodemax;
0273 input->keycode = iqs62x_keys->keycode;
0274 input->keycodesize = sizeof(*iqs62x_keys->keycode);
0275
0276 input->name = iqs62x->dev_desc->dev_name;
0277 input->id.bustype = BUS_I2C;
0278
0279 for (i = 0; i < iqs62x_keys->keycodemax; i++)
0280 if (iqs62x_keys->keycode[i] != KEY_RESERVED)
0281 input_set_capability(input, EV_KEY,
0282 iqs62x_keys->keycode[i]);
0283
0284 for (i = 0; i < ARRAY_SIZE(iqs62x_keys->switches); i++)
0285 if (iqs62x_keys->switches[i].enabled)
0286 input_set_capability(input, EV_SW,
0287 iqs62x_keys->switches[i].code);
0288
0289 iqs62x_keys->iqs62x = iqs62x;
0290 iqs62x_keys->input = input;
0291
0292 ret = iqs62x_keys_init(iqs62x_keys);
0293 if (ret) {
0294 dev_err(&pdev->dev, "Failed to initialize device: %d\n", ret);
0295 return ret;
0296 }
0297
0298 ret = input_register_device(iqs62x_keys->input);
0299 if (ret) {
0300 dev_err(&pdev->dev, "Failed to register device: %d\n", ret);
0301 return ret;
0302 }
0303
0304 iqs62x_keys->notifier.notifier_call = iqs62x_keys_notifier;
0305 ret = blocking_notifier_chain_register(&iqs62x_keys->iqs62x->nh,
0306 &iqs62x_keys->notifier);
0307 if (ret)
0308 dev_err(&pdev->dev, "Failed to register notifier: %d\n", ret);
0309
0310 return ret;
0311 }
0312
0313 static int iqs62x_keys_remove(struct platform_device *pdev)
0314 {
0315 struct iqs62x_keys_private *iqs62x_keys = platform_get_drvdata(pdev);
0316 int ret;
0317
0318 ret = blocking_notifier_chain_unregister(&iqs62x_keys->iqs62x->nh,
0319 &iqs62x_keys->notifier);
0320 if (ret)
0321 dev_err(&pdev->dev, "Failed to unregister notifier: %d\n", ret);
0322
0323 return ret;
0324 }
0325
0326 static struct platform_driver iqs62x_keys_platform_driver = {
0327 .driver = {
0328 .name = "iqs62x-keys",
0329 },
0330 .probe = iqs62x_keys_probe,
0331 .remove = iqs62x_keys_remove,
0332 };
0333 module_platform_driver(iqs62x_keys_platform_driver);
0334
0335 MODULE_AUTHOR("Jeff LaBundy <jeff@labundy.com>");
0336 MODULE_DESCRIPTION("Azoteq IQS620A/621/622/624/625 Keys and Switches");
0337 MODULE_LICENSE("GPL");
0338 MODULE_ALIAS("platform:iqs62x-keys");