cap1106.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * Input driver for Microchip CAP1106, 6 channel capacitive touch sensor
  3. *
  4. * http://www.microchip.com/wwwproducts/Devices.aspx?product=CAP1106
  5. *
  6. * (c) 2014 Daniel Mack <linux@zonque.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/input.h>
  16. #include <linux/of_irq.h>
  17. #include <linux/regmap.h>
  18. #include <linux/i2c.h>
  19. #include <linux/gpio/consumer.h>
  20. #define CAP1106_REG_MAIN_CONTROL 0x00
  21. #define CAP1106_REG_MAIN_CONTROL_GAIN_SHIFT (6)
  22. #define CAP1106_REG_MAIN_CONTROL_GAIN_MASK (0xc0)
  23. #define CAP1106_REG_MAIN_CONTROL_DLSEEP BIT(4)
  24. #define CAP1106_REG_GENERAL_STATUS 0x02
  25. #define CAP1106_REG_SENSOR_INPUT 0x03
  26. #define CAP1106_REG_NOISE_FLAG_STATUS 0x0a
  27. #define CAP1106_REG_SENOR_DELTA(X) (0x10 + (X))
  28. #define CAP1106_REG_SENSITIVITY_CONTROL 0x1f
  29. #define CAP1106_REG_CONFIG 0x20
  30. #define CAP1106_REG_SENSOR_ENABLE 0x21
  31. #define CAP1106_REG_SENSOR_CONFIG 0x22
  32. #define CAP1106_REG_SENSOR_CONFIG2 0x23
  33. #define CAP1106_REG_SAMPLING_CONFIG 0x24
  34. #define CAP1106_REG_CALIBRATION 0x26
  35. #define CAP1106_REG_INT_ENABLE 0x27
  36. #define CAP1106_REG_REPEAT_RATE 0x28
  37. #define CAP1106_REG_MT_CONFIG 0x2a
  38. #define CAP1106_REG_MT_PATTERN_CONFIG 0x2b
  39. #define CAP1106_REG_MT_PATTERN 0x2d
  40. #define CAP1106_REG_RECALIB_CONFIG 0x2f
  41. #define CAP1106_REG_SENSOR_THRESH(X) (0x30 + (X))
  42. #define CAP1106_REG_SENSOR_NOISE_THRESH 0x38
  43. #define CAP1106_REG_STANDBY_CHANNEL 0x40
  44. #define CAP1106_REG_STANDBY_CONFIG 0x41
  45. #define CAP1106_REG_STANDBY_SENSITIVITY 0x42
  46. #define CAP1106_REG_STANDBY_THRESH 0x43
  47. #define CAP1106_REG_CONFIG2 0x44
  48. #define CAP1106_REG_SENSOR_BASE_CNT(X) (0x50 + (X))
  49. #define CAP1106_REG_SENSOR_CALIB (0xb1 + (X))
  50. #define CAP1106_REG_SENSOR_CALIB_LSB1 0xb9
  51. #define CAP1106_REG_SENSOR_CALIB_LSB2 0xba
  52. #define CAP1106_REG_PRODUCT_ID 0xfd
  53. #define CAP1106_REG_MANUFACTURER_ID 0xfe
  54. #define CAP1106_REG_REVISION 0xff
  55. #define CAP1106_NUM_CHN 6
  56. #define CAP1106_PRODUCT_ID 0x55
  57. #define CAP1106_MANUFACTURER_ID 0x5d
  58. struct cap1106_priv {
  59. struct regmap *regmap;
  60. struct input_dev *idev;
  61. /* config */
  62. unsigned short keycodes[CAP1106_NUM_CHN];
  63. };
  64. static const struct reg_default cap1106_reg_defaults[] = {
  65. { CAP1106_REG_MAIN_CONTROL, 0x00 },
  66. { CAP1106_REG_GENERAL_STATUS, 0x00 },
  67. { CAP1106_REG_SENSOR_INPUT, 0x00 },
  68. { CAP1106_REG_NOISE_FLAG_STATUS, 0x00 },
  69. { CAP1106_REG_SENSITIVITY_CONTROL, 0x2f },
  70. { CAP1106_REG_CONFIG, 0x20 },
  71. { CAP1106_REG_SENSOR_ENABLE, 0x3f },
  72. { CAP1106_REG_SENSOR_CONFIG, 0xa4 },
  73. { CAP1106_REG_SENSOR_CONFIG2, 0x07 },
  74. { CAP1106_REG_SAMPLING_CONFIG, 0x39 },
  75. { CAP1106_REG_CALIBRATION, 0x00 },
  76. { CAP1106_REG_INT_ENABLE, 0x3f },
  77. { CAP1106_REG_REPEAT_RATE, 0x3f },
  78. { CAP1106_REG_MT_CONFIG, 0x80 },
  79. { CAP1106_REG_MT_PATTERN_CONFIG, 0x00 },
  80. { CAP1106_REG_MT_PATTERN, 0x3f },
  81. { CAP1106_REG_RECALIB_CONFIG, 0x8a },
  82. { CAP1106_REG_SENSOR_THRESH(0), 0x40 },
  83. { CAP1106_REG_SENSOR_THRESH(1), 0x40 },
  84. { CAP1106_REG_SENSOR_THRESH(2), 0x40 },
  85. { CAP1106_REG_SENSOR_THRESH(3), 0x40 },
  86. { CAP1106_REG_SENSOR_THRESH(4), 0x40 },
  87. { CAP1106_REG_SENSOR_THRESH(5), 0x40 },
  88. { CAP1106_REG_SENSOR_NOISE_THRESH, 0x01 },
  89. { CAP1106_REG_STANDBY_CHANNEL, 0x00 },
  90. { CAP1106_REG_STANDBY_CONFIG, 0x39 },
  91. { CAP1106_REG_STANDBY_SENSITIVITY, 0x02 },
  92. { CAP1106_REG_STANDBY_THRESH, 0x40 },
  93. { CAP1106_REG_CONFIG2, 0x40 },
  94. { CAP1106_REG_SENSOR_CALIB_LSB1, 0x00 },
  95. { CAP1106_REG_SENSOR_CALIB_LSB2, 0x00 },
  96. };
  97. static bool cap1106_volatile_reg(struct device *dev, unsigned int reg)
  98. {
  99. switch (reg) {
  100. case CAP1106_REG_MAIN_CONTROL:
  101. case CAP1106_REG_SENSOR_INPUT:
  102. case CAP1106_REG_SENOR_DELTA(0):
  103. case CAP1106_REG_SENOR_DELTA(1):
  104. case CAP1106_REG_SENOR_DELTA(2):
  105. case CAP1106_REG_SENOR_DELTA(3):
  106. case CAP1106_REG_SENOR_DELTA(4):
  107. case CAP1106_REG_SENOR_DELTA(5):
  108. case CAP1106_REG_PRODUCT_ID:
  109. case CAP1106_REG_MANUFACTURER_ID:
  110. case CAP1106_REG_REVISION:
  111. return true;
  112. }
  113. return false;
  114. }
  115. static const struct regmap_config cap1106_regmap_config = {
  116. .reg_bits = 8,
  117. .val_bits = 8,
  118. .max_register = CAP1106_REG_REVISION,
  119. .reg_defaults = cap1106_reg_defaults,
  120. .num_reg_defaults = ARRAY_SIZE(cap1106_reg_defaults),
  121. .cache_type = REGCACHE_RBTREE,
  122. .volatile_reg = cap1106_volatile_reg,
  123. };
  124. static irqreturn_t cap1106_thread_func(int irq_num, void *data)
  125. {
  126. struct cap1106_priv *priv = data;
  127. unsigned int status;
  128. int ret, i;
  129. /*
  130. * Deassert interrupt. This needs to be done before reading the status
  131. * registers, which will not carry valid values otherwise.
  132. */
  133. ret = regmap_update_bits(priv->regmap, CAP1106_REG_MAIN_CONTROL, 1, 0);
  134. if (ret < 0)
  135. goto out;
  136. ret = regmap_read(priv->regmap, CAP1106_REG_SENSOR_INPUT, &status);
  137. if (ret < 0)
  138. goto out;
  139. for (i = 0; i < CAP1106_NUM_CHN; i++)
  140. input_report_key(priv->idev, priv->keycodes[i],
  141. status & (1 << i));
  142. input_sync(priv->idev);
  143. out:
  144. return IRQ_HANDLED;
  145. }
  146. static int cap1106_set_sleep(struct cap1106_priv *priv, bool sleep)
  147. {
  148. return regmap_update_bits(priv->regmap, CAP1106_REG_MAIN_CONTROL,
  149. CAP1106_REG_MAIN_CONTROL_DLSEEP,
  150. sleep ? CAP1106_REG_MAIN_CONTROL_DLSEEP : 0);
  151. }
  152. static int cap1106_input_open(struct input_dev *idev)
  153. {
  154. struct cap1106_priv *priv = input_get_drvdata(idev);
  155. return cap1106_set_sleep(priv, false);
  156. }
  157. static void cap1106_input_close(struct input_dev *idev)
  158. {
  159. struct cap1106_priv *priv = input_get_drvdata(idev);
  160. cap1106_set_sleep(priv, true);
  161. }
  162. static int cap1106_i2c_probe(struct i2c_client *i2c_client,
  163. const struct i2c_device_id *id)
  164. {
  165. struct device *dev = &i2c_client->dev;
  166. struct cap1106_priv *priv;
  167. struct device_node *node;
  168. int i, error, irq, gain = 0;
  169. unsigned int val, rev;
  170. u32 gain32, keycodes[CAP1106_NUM_CHN];
  171. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  172. if (!priv)
  173. return -ENOMEM;
  174. priv->regmap = devm_regmap_init_i2c(i2c_client, &cap1106_regmap_config);
  175. if (IS_ERR(priv->regmap))
  176. return PTR_ERR(priv->regmap);
  177. error = regmap_read(priv->regmap, CAP1106_REG_PRODUCT_ID, &val);
  178. if (error)
  179. return error;
  180. if (val != CAP1106_PRODUCT_ID) {
  181. dev_err(dev, "Product ID: Got 0x%02x, expected 0x%02x\n",
  182. val, CAP1106_PRODUCT_ID);
  183. return -ENODEV;
  184. }
  185. error = regmap_read(priv->regmap, CAP1106_REG_MANUFACTURER_ID, &val);
  186. if (error)
  187. return error;
  188. if (val != CAP1106_MANUFACTURER_ID) {
  189. dev_err(dev, "Manufacturer ID: Got 0x%02x, expected 0x%02x\n",
  190. val, CAP1106_MANUFACTURER_ID);
  191. return -ENODEV;
  192. }
  193. error = regmap_read(priv->regmap, CAP1106_REG_REVISION, &rev);
  194. if (error < 0)
  195. return error;
  196. dev_info(dev, "CAP1106 detected, revision 0x%02x\n", rev);
  197. i2c_set_clientdata(i2c_client, priv);
  198. node = dev->of_node;
  199. if (!of_property_read_u32(node, "microchip,sensor-gain", &gain32)) {
  200. if (is_power_of_2(gain32) && gain32 <= 8)
  201. gain = ilog2(gain32);
  202. else
  203. dev_err(dev, "Invalid sensor-gain value %d\n", gain32);
  204. }
  205. BUILD_BUG_ON(ARRAY_SIZE(keycodes) != ARRAY_SIZE(priv->keycodes));
  206. /* Provide some useful defaults */
  207. for (i = 0; i < ARRAY_SIZE(keycodes); i++)
  208. keycodes[i] = KEY_A + i;
  209. of_property_read_u32_array(node, "linux,keycodes",
  210. keycodes, ARRAY_SIZE(keycodes));
  211. for (i = 0; i < ARRAY_SIZE(keycodes); i++)
  212. priv->keycodes[i] = keycodes[i];
  213. error = regmap_update_bits(priv->regmap, CAP1106_REG_MAIN_CONTROL,
  214. CAP1106_REG_MAIN_CONTROL_GAIN_MASK,
  215. gain << CAP1106_REG_MAIN_CONTROL_GAIN_SHIFT);
  216. if (error)
  217. return error;
  218. /* Disable autorepeat. The Linux input system has its own handling. */
  219. error = regmap_write(priv->regmap, CAP1106_REG_REPEAT_RATE, 0);
  220. if (error)
  221. return error;
  222. priv->idev = devm_input_allocate_device(dev);
  223. if (!priv->idev)
  224. return -ENOMEM;
  225. priv->idev->name = "CAP1106 capacitive touch sensor";
  226. priv->idev->id.bustype = BUS_I2C;
  227. priv->idev->evbit[0] = BIT_MASK(EV_KEY);
  228. if (of_property_read_bool(node, "autorepeat"))
  229. __set_bit(EV_REP, priv->idev->evbit);
  230. for (i = 0; i < CAP1106_NUM_CHN; i++)
  231. __set_bit(priv->keycodes[i], priv->idev->keybit);
  232. __clear_bit(KEY_RESERVED, priv->idev->keybit);
  233. priv->idev->keycode = priv->keycodes;
  234. priv->idev->keycodesize = sizeof(priv->keycodes[0]);
  235. priv->idev->keycodemax = ARRAY_SIZE(priv->keycodes);
  236. priv->idev->id.vendor = CAP1106_MANUFACTURER_ID;
  237. priv->idev->id.product = CAP1106_PRODUCT_ID;
  238. priv->idev->id.version = rev;
  239. priv->idev->open = cap1106_input_open;
  240. priv->idev->close = cap1106_input_close;
  241. input_set_drvdata(priv->idev, priv);
  242. /*
  243. * Put the device in deep sleep mode for now.
  244. * ->open() will bring it back once the it is actually needed.
  245. */
  246. cap1106_set_sleep(priv, true);
  247. error = input_register_device(priv->idev);
  248. if (error)
  249. return error;
  250. irq = irq_of_parse_and_map(node, 0);
  251. if (!irq) {
  252. dev_err(dev, "Unable to parse or map IRQ\n");
  253. return -ENXIO;
  254. }
  255. error = devm_request_threaded_irq(dev, irq, NULL, cap1106_thread_func,
  256. IRQF_ONESHOT, dev_name(dev), priv);
  257. if (error)
  258. return error;
  259. return 0;
  260. }
  261. static const struct of_device_id cap1106_dt_ids[] = {
  262. { .compatible = "microchip,cap1106", },
  263. {}
  264. };
  265. MODULE_DEVICE_TABLE(of, cap1106_dt_ids);
  266. static const struct i2c_device_id cap1106_i2c_ids[] = {
  267. { "cap1106", 0 },
  268. {}
  269. };
  270. MODULE_DEVICE_TABLE(i2c, cap1106_i2c_ids);
  271. static struct i2c_driver cap1106_i2c_driver = {
  272. .driver = {
  273. .name = "cap1106",
  274. .owner = THIS_MODULE,
  275. .of_match_table = cap1106_dt_ids,
  276. },
  277. .id_table = cap1106_i2c_ids,
  278. .probe = cap1106_i2c_probe,
  279. };
  280. module_i2c_driver(cap1106_i2c_driver);
  281. MODULE_ALIAS("platform:cap1106");
  282. MODULE_DESCRIPTION("Microchip CAP1106 driver");
  283. MODULE_AUTHOR("Daniel Mack <linux@zonque.org>");
  284. MODULE_LICENSE("GPL v2");