cap11xx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /*
  2. * Input driver for Microchip CAP11xx based capacitive touch sensors
  3. *
  4. * (c) 2014 Daniel Mack <linux@zonque.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/input.h>
  14. #include <linux/leds.h>
  15. #include <linux/of_irq.h>
  16. #include <linux/regmap.h>
  17. #include <linux/i2c.h>
  18. #include <linux/gpio/consumer.h>
  19. #define CAP11XX_REG_MAIN_CONTROL 0x00
  20. #define CAP11XX_REG_MAIN_CONTROL_GAIN_SHIFT (6)
  21. #define CAP11XX_REG_MAIN_CONTROL_GAIN_MASK (0xc0)
  22. #define CAP11XX_REG_MAIN_CONTROL_DLSEEP BIT(4)
  23. #define CAP11XX_REG_GENERAL_STATUS 0x02
  24. #define CAP11XX_REG_SENSOR_INPUT 0x03
  25. #define CAP11XX_REG_NOISE_FLAG_STATUS 0x0a
  26. #define CAP11XX_REG_SENOR_DELTA(X) (0x10 + (X))
  27. #define CAP11XX_REG_SENSITIVITY_CONTROL 0x1f
  28. #define CAP11XX_REG_CONFIG 0x20
  29. #define CAP11XX_REG_SENSOR_ENABLE 0x21
  30. #define CAP11XX_REG_SENSOR_CONFIG 0x22
  31. #define CAP11XX_REG_SENSOR_CONFIG2 0x23
  32. #define CAP11XX_REG_SAMPLING_CONFIG 0x24
  33. #define CAP11XX_REG_CALIBRATION 0x26
  34. #define CAP11XX_REG_INT_ENABLE 0x27
  35. #define CAP11XX_REG_REPEAT_RATE 0x28
  36. #define CAP11XX_REG_MT_CONFIG 0x2a
  37. #define CAP11XX_REG_MT_PATTERN_CONFIG 0x2b
  38. #define CAP11XX_REG_MT_PATTERN 0x2d
  39. #define CAP11XX_REG_RECALIB_CONFIG 0x2f
  40. #define CAP11XX_REG_SENSOR_THRESH(X) (0x30 + (X))
  41. #define CAP11XX_REG_SENSOR_NOISE_THRESH 0x38
  42. #define CAP11XX_REG_STANDBY_CHANNEL 0x40
  43. #define CAP11XX_REG_STANDBY_CONFIG 0x41
  44. #define CAP11XX_REG_STANDBY_SENSITIVITY 0x42
  45. #define CAP11XX_REG_STANDBY_THRESH 0x43
  46. #define CAP11XX_REG_CONFIG2 0x44
  47. #define CAP11XX_REG_CONFIG2_ALT_POL BIT(6)
  48. #define CAP11XX_REG_SENSOR_BASE_CNT(X) (0x50 + (X))
  49. #define CAP11XX_REG_LED_POLARITY 0x73
  50. #define CAP11XX_REG_LED_OUTPUT_CONTROL 0x74
  51. #define CAP11XX_REG_LED_DUTY_CYCLE_1 0x90
  52. #define CAP11XX_REG_LED_DUTY_CYCLE_2 0x91
  53. #define CAP11XX_REG_LED_DUTY_CYCLE_3 0x92
  54. #define CAP11XX_REG_LED_DUTY_CYCLE_4 0x93
  55. #define CAP11XX_REG_LED_DUTY_MIN_MASK (0x0f)
  56. #define CAP11XX_REG_LED_DUTY_MIN_MASK_SHIFT (0)
  57. #define CAP11XX_REG_LED_DUTY_MAX_MASK (0xf0)
  58. #define CAP11XX_REG_LED_DUTY_MAX_MASK_SHIFT (4)
  59. #define CAP11XX_REG_LED_DUTY_MAX_VALUE (15)
  60. #define CAP11XX_REG_SENSOR_CALIB (0xb1 + (X))
  61. #define CAP11XX_REG_SENSOR_CALIB_LSB1 0xb9
  62. #define CAP11XX_REG_SENSOR_CALIB_LSB2 0xba
  63. #define CAP11XX_REG_PRODUCT_ID 0xfd
  64. #define CAP11XX_REG_MANUFACTURER_ID 0xfe
  65. #define CAP11XX_REG_REVISION 0xff
  66. #define CAP11XX_MANUFACTURER_ID 0x5d
  67. #ifdef CONFIG_LEDS_CLASS
  68. struct cap11xx_led {
  69. struct cap11xx_priv *priv;
  70. struct led_classdev cdev;
  71. struct work_struct work;
  72. u32 reg;
  73. enum led_brightness new_brightness;
  74. };
  75. #endif
  76. struct cap11xx_priv {
  77. struct regmap *regmap;
  78. struct input_dev *idev;
  79. struct cap11xx_led *leds;
  80. int num_leds;
  81. /* config */
  82. u32 keycodes[];
  83. };
  84. struct cap11xx_hw_model {
  85. u8 product_id;
  86. unsigned int num_channels;
  87. unsigned int num_leds;
  88. };
  89. enum {
  90. CAP1106,
  91. CAP1126,
  92. CAP1188,
  93. };
  94. static const struct cap11xx_hw_model cap11xx_devices[] = {
  95. [CAP1106] = { .product_id = 0x55, .num_channels = 6, .num_leds = 0 },
  96. [CAP1126] = { .product_id = 0x53, .num_channels = 6, .num_leds = 2 },
  97. [CAP1188] = { .product_id = 0x50, .num_channels = 8, .num_leds = 8 },
  98. };
  99. static const struct reg_default cap11xx_reg_defaults[] = {
  100. { CAP11XX_REG_MAIN_CONTROL, 0x00 },
  101. { CAP11XX_REG_GENERAL_STATUS, 0x00 },
  102. { CAP11XX_REG_SENSOR_INPUT, 0x00 },
  103. { CAP11XX_REG_NOISE_FLAG_STATUS, 0x00 },
  104. { CAP11XX_REG_SENSITIVITY_CONTROL, 0x2f },
  105. { CAP11XX_REG_CONFIG, 0x20 },
  106. { CAP11XX_REG_SENSOR_ENABLE, 0x3f },
  107. { CAP11XX_REG_SENSOR_CONFIG, 0xa4 },
  108. { CAP11XX_REG_SENSOR_CONFIG2, 0x07 },
  109. { CAP11XX_REG_SAMPLING_CONFIG, 0x39 },
  110. { CAP11XX_REG_CALIBRATION, 0x00 },
  111. { CAP11XX_REG_INT_ENABLE, 0x3f },
  112. { CAP11XX_REG_REPEAT_RATE, 0x3f },
  113. { CAP11XX_REG_MT_CONFIG, 0x80 },
  114. { CAP11XX_REG_MT_PATTERN_CONFIG, 0x00 },
  115. { CAP11XX_REG_MT_PATTERN, 0x3f },
  116. { CAP11XX_REG_RECALIB_CONFIG, 0x8a },
  117. { CAP11XX_REG_SENSOR_THRESH(0), 0x40 },
  118. { CAP11XX_REG_SENSOR_THRESH(1), 0x40 },
  119. { CAP11XX_REG_SENSOR_THRESH(2), 0x40 },
  120. { CAP11XX_REG_SENSOR_THRESH(3), 0x40 },
  121. { CAP11XX_REG_SENSOR_THRESH(4), 0x40 },
  122. { CAP11XX_REG_SENSOR_THRESH(5), 0x40 },
  123. { CAP11XX_REG_SENSOR_NOISE_THRESH, 0x01 },
  124. { CAP11XX_REG_STANDBY_CHANNEL, 0x00 },
  125. { CAP11XX_REG_STANDBY_CONFIG, 0x39 },
  126. { CAP11XX_REG_STANDBY_SENSITIVITY, 0x02 },
  127. { CAP11XX_REG_STANDBY_THRESH, 0x40 },
  128. { CAP11XX_REG_CONFIG2, 0x40 },
  129. { CAP11XX_REG_LED_POLARITY, 0x00 },
  130. { CAP11XX_REG_SENSOR_CALIB_LSB1, 0x00 },
  131. { CAP11XX_REG_SENSOR_CALIB_LSB2, 0x00 },
  132. };
  133. static bool cap11xx_volatile_reg(struct device *dev, unsigned int reg)
  134. {
  135. switch (reg) {
  136. case CAP11XX_REG_MAIN_CONTROL:
  137. case CAP11XX_REG_SENSOR_INPUT:
  138. case CAP11XX_REG_SENOR_DELTA(0):
  139. case CAP11XX_REG_SENOR_DELTA(1):
  140. case CAP11XX_REG_SENOR_DELTA(2):
  141. case CAP11XX_REG_SENOR_DELTA(3):
  142. case CAP11XX_REG_SENOR_DELTA(4):
  143. case CAP11XX_REG_SENOR_DELTA(5):
  144. case CAP11XX_REG_PRODUCT_ID:
  145. case CAP11XX_REG_MANUFACTURER_ID:
  146. case CAP11XX_REG_REVISION:
  147. return true;
  148. }
  149. return false;
  150. }
  151. static const struct regmap_config cap11xx_regmap_config = {
  152. .reg_bits = 8,
  153. .val_bits = 8,
  154. .max_register = CAP11XX_REG_REVISION,
  155. .reg_defaults = cap11xx_reg_defaults,
  156. .num_reg_defaults = ARRAY_SIZE(cap11xx_reg_defaults),
  157. .cache_type = REGCACHE_RBTREE,
  158. .volatile_reg = cap11xx_volatile_reg,
  159. };
  160. static irqreturn_t cap11xx_thread_func(int irq_num, void *data)
  161. {
  162. struct cap11xx_priv *priv = data;
  163. unsigned int status;
  164. int ret, i;
  165. /*
  166. * Deassert interrupt. This needs to be done before reading the status
  167. * registers, which will not carry valid values otherwise.
  168. */
  169. ret = regmap_update_bits(priv->regmap, CAP11XX_REG_MAIN_CONTROL, 1, 0);
  170. if (ret < 0)
  171. goto out;
  172. ret = regmap_read(priv->regmap, CAP11XX_REG_SENSOR_INPUT, &status);
  173. if (ret < 0)
  174. goto out;
  175. for (i = 0; i < priv->idev->keycodemax; i++)
  176. input_report_key(priv->idev, priv->keycodes[i],
  177. status & (1 << i));
  178. input_sync(priv->idev);
  179. out:
  180. return IRQ_HANDLED;
  181. }
  182. static int cap11xx_set_sleep(struct cap11xx_priv *priv, bool sleep)
  183. {
  184. /*
  185. * DLSEEP mode will turn off all LEDS, prevent this
  186. */
  187. if (IS_ENABLED(CONFIG_LEDS_CLASS) && priv->num_leds)
  188. return 0;
  189. return regmap_update_bits(priv->regmap, CAP11XX_REG_MAIN_CONTROL,
  190. CAP11XX_REG_MAIN_CONTROL_DLSEEP,
  191. sleep ? CAP11XX_REG_MAIN_CONTROL_DLSEEP : 0);
  192. }
  193. static int cap11xx_input_open(struct input_dev *idev)
  194. {
  195. struct cap11xx_priv *priv = input_get_drvdata(idev);
  196. return cap11xx_set_sleep(priv, false);
  197. }
  198. static void cap11xx_input_close(struct input_dev *idev)
  199. {
  200. struct cap11xx_priv *priv = input_get_drvdata(idev);
  201. cap11xx_set_sleep(priv, true);
  202. }
  203. #ifdef CONFIG_LEDS_CLASS
  204. static void cap11xx_led_work(struct work_struct *work)
  205. {
  206. struct cap11xx_led *led = container_of(work, struct cap11xx_led, work);
  207. struct cap11xx_priv *priv = led->priv;
  208. int value = led->new_brightness;
  209. /*
  210. * All LEDs share the same duty cycle as this is a HW limitation.
  211. * Brightness levels per LED are either 0 (OFF) and 1 (ON).
  212. */
  213. regmap_update_bits(priv->regmap, CAP11XX_REG_LED_OUTPUT_CONTROL,
  214. BIT(led->reg), value ? BIT(led->reg) : 0);
  215. }
  216. static void cap11xx_led_set(struct led_classdev *cdev,
  217. enum led_brightness value)
  218. {
  219. struct cap11xx_led *led = container_of(cdev, struct cap11xx_led, cdev);
  220. if (led->new_brightness == value)
  221. return;
  222. led->new_brightness = value;
  223. schedule_work(&led->work);
  224. }
  225. static int cap11xx_init_leds(struct device *dev,
  226. struct cap11xx_priv *priv, int num_leds)
  227. {
  228. struct device_node *node = dev->of_node, *child;
  229. struct cap11xx_led *led;
  230. int cnt = of_get_child_count(node);
  231. int error;
  232. if (!num_leds || !cnt)
  233. return 0;
  234. if (cnt > num_leds)
  235. return -EINVAL;
  236. led = devm_kcalloc(dev, cnt, sizeof(struct cap11xx_led), GFP_KERNEL);
  237. if (!led)
  238. return -ENOMEM;
  239. priv->leds = led;
  240. error = regmap_update_bits(priv->regmap,
  241. CAP11XX_REG_LED_OUTPUT_CONTROL, 0xff, 0);
  242. if (error)
  243. return error;
  244. error = regmap_update_bits(priv->regmap, CAP11XX_REG_LED_DUTY_CYCLE_4,
  245. CAP11XX_REG_LED_DUTY_MAX_MASK,
  246. CAP11XX_REG_LED_DUTY_MAX_VALUE <<
  247. CAP11XX_REG_LED_DUTY_MAX_MASK_SHIFT);
  248. if (error)
  249. return error;
  250. for_each_child_of_node(node, child) {
  251. u32 reg;
  252. led->cdev.name =
  253. of_get_property(child, "label", NULL) ? : child->name;
  254. led->cdev.default_trigger =
  255. of_get_property(child, "linux,default-trigger", NULL);
  256. led->cdev.flags = 0;
  257. led->cdev.brightness_set = cap11xx_led_set;
  258. led->cdev.max_brightness = 1;
  259. led->cdev.brightness = LED_OFF;
  260. error = of_property_read_u32(child, "reg", &reg);
  261. if (error != 0 || reg >= num_leds)
  262. return -EINVAL;
  263. led->reg = reg;
  264. led->priv = priv;
  265. INIT_WORK(&led->work, cap11xx_led_work);
  266. error = devm_led_classdev_register(dev, &led->cdev);
  267. if (error)
  268. return error;
  269. priv->num_leds++;
  270. led++;
  271. }
  272. return 0;
  273. }
  274. #else
  275. static int cap11xx_init_leds(struct device *dev,
  276. struct cap11xx_priv *priv, int num_leds)
  277. {
  278. return 0;
  279. }
  280. #endif
  281. static int cap11xx_i2c_probe(struct i2c_client *i2c_client,
  282. const struct i2c_device_id *id)
  283. {
  284. struct device *dev = &i2c_client->dev;
  285. struct cap11xx_priv *priv;
  286. struct device_node *node;
  287. const struct cap11xx_hw_model *cap;
  288. int i, error, irq, gain = 0;
  289. unsigned int val, rev;
  290. u32 gain32;
  291. if (id->driver_data >= ARRAY_SIZE(cap11xx_devices)) {
  292. dev_err(dev, "Invalid device ID %lu\n", id->driver_data);
  293. return -EINVAL;
  294. }
  295. cap = &cap11xx_devices[id->driver_data];
  296. if (!cap || !cap->num_channels) {
  297. dev_err(dev, "Invalid device configuration\n");
  298. return -EINVAL;
  299. }
  300. priv = devm_kzalloc(dev,
  301. sizeof(*priv) +
  302. cap->num_channels * sizeof(priv->keycodes[0]),
  303. GFP_KERNEL);
  304. if (!priv)
  305. return -ENOMEM;
  306. priv->regmap = devm_regmap_init_i2c(i2c_client, &cap11xx_regmap_config);
  307. if (IS_ERR(priv->regmap))
  308. return PTR_ERR(priv->regmap);
  309. error = regmap_read(priv->regmap, CAP11XX_REG_PRODUCT_ID, &val);
  310. if (error)
  311. return error;
  312. if (val != cap->product_id) {
  313. dev_err(dev, "Product ID: Got 0x%02x, expected 0x%02x\n",
  314. val, cap->product_id);
  315. return -ENXIO;
  316. }
  317. error = regmap_read(priv->regmap, CAP11XX_REG_MANUFACTURER_ID, &val);
  318. if (error)
  319. return error;
  320. if (val != CAP11XX_MANUFACTURER_ID) {
  321. dev_err(dev, "Manufacturer ID: Got 0x%02x, expected 0x%02x\n",
  322. val, CAP11XX_MANUFACTURER_ID);
  323. return -ENXIO;
  324. }
  325. error = regmap_read(priv->regmap, CAP11XX_REG_REVISION, &rev);
  326. if (error < 0)
  327. return error;
  328. dev_info(dev, "CAP11XX detected, revision 0x%02x\n", rev);
  329. i2c_set_clientdata(i2c_client, priv);
  330. node = dev->of_node;
  331. if (!of_property_read_u32(node, "microchip,sensor-gain", &gain32)) {
  332. if (is_power_of_2(gain32) && gain32 <= 8)
  333. gain = ilog2(gain32);
  334. else
  335. dev_err(dev, "Invalid sensor-gain value %d\n", gain32);
  336. }
  337. if (of_property_read_bool(node, "microchip,irq-active-high")) {
  338. error = regmap_update_bits(priv->regmap, CAP11XX_REG_CONFIG2,
  339. CAP11XX_REG_CONFIG2_ALT_POL, 0);
  340. if (error)
  341. return error;
  342. }
  343. /* Provide some useful defaults */
  344. for (i = 0; i < cap->num_channels; i++)
  345. priv->keycodes[i] = KEY_A + i;
  346. of_property_read_u32_array(node, "linux,keycodes",
  347. priv->keycodes, cap->num_channels);
  348. error = regmap_update_bits(priv->regmap, CAP11XX_REG_MAIN_CONTROL,
  349. CAP11XX_REG_MAIN_CONTROL_GAIN_MASK,
  350. gain << CAP11XX_REG_MAIN_CONTROL_GAIN_SHIFT);
  351. if (error)
  352. return error;
  353. /* Disable autorepeat. The Linux input system has its own handling. */
  354. error = regmap_write(priv->regmap, CAP11XX_REG_REPEAT_RATE, 0);
  355. if (error)
  356. return error;
  357. priv->idev = devm_input_allocate_device(dev);
  358. if (!priv->idev)
  359. return -ENOMEM;
  360. priv->idev->name = "CAP11XX capacitive touch sensor";
  361. priv->idev->id.bustype = BUS_I2C;
  362. priv->idev->evbit[0] = BIT_MASK(EV_KEY);
  363. if (of_property_read_bool(node, "autorepeat"))
  364. __set_bit(EV_REP, priv->idev->evbit);
  365. for (i = 0; i < cap->num_channels; i++)
  366. __set_bit(priv->keycodes[i], priv->idev->keybit);
  367. __clear_bit(KEY_RESERVED, priv->idev->keybit);
  368. priv->idev->keycode = priv->keycodes;
  369. priv->idev->keycodesize = sizeof(priv->keycodes[0]);
  370. priv->idev->keycodemax = cap->num_channels;
  371. priv->idev->id.vendor = CAP11XX_MANUFACTURER_ID;
  372. priv->idev->id.product = cap->product_id;
  373. priv->idev->id.version = rev;
  374. priv->idev->open = cap11xx_input_open;
  375. priv->idev->close = cap11xx_input_close;
  376. error = cap11xx_init_leds(dev, priv, cap->num_leds);
  377. if (error)
  378. return error;
  379. input_set_drvdata(priv->idev, priv);
  380. /*
  381. * Put the device in deep sleep mode for now.
  382. * ->open() will bring it back once the it is actually needed.
  383. */
  384. cap11xx_set_sleep(priv, true);
  385. error = input_register_device(priv->idev);
  386. if (error)
  387. return error;
  388. irq = irq_of_parse_and_map(node, 0);
  389. if (!irq) {
  390. dev_err(dev, "Unable to parse or map IRQ\n");
  391. return -ENXIO;
  392. }
  393. error = devm_request_threaded_irq(dev, irq, NULL, cap11xx_thread_func,
  394. IRQF_ONESHOT, dev_name(dev), priv);
  395. if (error)
  396. return error;
  397. return 0;
  398. }
  399. static const struct of_device_id cap11xx_dt_ids[] = {
  400. { .compatible = "microchip,cap1106", },
  401. { .compatible = "microchip,cap1126", },
  402. { .compatible = "microchip,cap1188", },
  403. {}
  404. };
  405. MODULE_DEVICE_TABLE(of, cap11xx_dt_ids);
  406. static const struct i2c_device_id cap11xx_i2c_ids[] = {
  407. { "cap1106", CAP1106 },
  408. { "cap1126", CAP1126 },
  409. { "cap1188", CAP1188 },
  410. {}
  411. };
  412. MODULE_DEVICE_TABLE(i2c, cap11xx_i2c_ids);
  413. static struct i2c_driver cap11xx_i2c_driver = {
  414. .driver = {
  415. .name = "cap11xx",
  416. .of_match_table = cap11xx_dt_ids,
  417. },
  418. .id_table = cap11xx_i2c_ids,
  419. .probe = cap11xx_i2c_probe,
  420. };
  421. module_i2c_driver(cap11xx_i2c_driver);
  422. MODULE_DESCRIPTION("Microchip CAP11XX driver");
  423. MODULE_AUTHOR("Daniel Mack <linux@zonque.org>");
  424. MODULE_LICENSE("GPL v2");