bu21013_ts.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. /*
  2. * Copyright (C) ST-Ericsson SA 2010
  3. * Author: Naveen Kumar G <naveen.gaddipati@stericsson.com> for ST-Ericsson
  4. * License terms:GNU General Public License (GPL) version 2
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/delay.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/i2c.h>
  10. #include <linux/workqueue.h>
  11. #include <linux/input.h>
  12. #include <linux/input/bu21013.h>
  13. #include <linux/slab.h>
  14. #include <linux/regulator/consumer.h>
  15. #include <linux/module.h>
  16. #include <linux/gpio.h>
  17. #include <linux/of.h>
  18. #include <linux/of_gpio.h>
  19. #define PEN_DOWN_INTR 0
  20. #define MAX_FINGERS 2
  21. #define RESET_DELAY 30
  22. #define PENUP_TIMEOUT (10)
  23. #define DELTA_MIN 16
  24. #define MASK_BITS 0x03
  25. #define SHIFT_8 8
  26. #define SHIFT_2 2
  27. #define LENGTH_OF_BUFFER 11
  28. #define I2C_RETRY_COUNT 5
  29. #define BU21013_SENSORS_BTN_0_7_REG 0x70
  30. #define BU21013_SENSORS_BTN_8_15_REG 0x71
  31. #define BU21013_SENSORS_BTN_16_23_REG 0x72
  32. #define BU21013_X1_POS_MSB_REG 0x73
  33. #define BU21013_X1_POS_LSB_REG 0x74
  34. #define BU21013_Y1_POS_MSB_REG 0x75
  35. #define BU21013_Y1_POS_LSB_REG 0x76
  36. #define BU21013_X2_POS_MSB_REG 0x77
  37. #define BU21013_X2_POS_LSB_REG 0x78
  38. #define BU21013_Y2_POS_MSB_REG 0x79
  39. #define BU21013_Y2_POS_LSB_REG 0x7A
  40. #define BU21013_INT_CLR_REG 0xE8
  41. #define BU21013_INT_MODE_REG 0xE9
  42. #define BU21013_GAIN_REG 0xEA
  43. #define BU21013_OFFSET_MODE_REG 0xEB
  44. #define BU21013_XY_EDGE_REG 0xEC
  45. #define BU21013_RESET_REG 0xED
  46. #define BU21013_CALIB_REG 0xEE
  47. #define BU21013_DONE_REG 0xEF
  48. #define BU21013_SENSOR_0_7_REG 0xF0
  49. #define BU21013_SENSOR_8_15_REG 0xF1
  50. #define BU21013_SENSOR_16_23_REG 0xF2
  51. #define BU21013_POS_MODE1_REG 0xF3
  52. #define BU21013_POS_MODE2_REG 0xF4
  53. #define BU21013_CLK_MODE_REG 0xF5
  54. #define BU21013_IDLE_REG 0xFA
  55. #define BU21013_FILTER_REG 0xFB
  56. #define BU21013_TH_ON_REG 0xFC
  57. #define BU21013_TH_OFF_REG 0xFD
  58. #define BU21013_RESET_ENABLE 0x01
  59. #define BU21013_SENSORS_EN_0_7 0x3F
  60. #define BU21013_SENSORS_EN_8_15 0xFC
  61. #define BU21013_SENSORS_EN_16_23 0x1F
  62. #define BU21013_POS_MODE1_0 0x02
  63. #define BU21013_POS_MODE1_1 0x04
  64. #define BU21013_POS_MODE1_2 0x08
  65. #define BU21013_POS_MODE2_ZERO 0x01
  66. #define BU21013_POS_MODE2_AVG1 0x02
  67. #define BU21013_POS_MODE2_AVG2 0x04
  68. #define BU21013_POS_MODE2_EN_XY 0x08
  69. #define BU21013_POS_MODE2_EN_RAW 0x10
  70. #define BU21013_POS_MODE2_MULTI 0x80
  71. #define BU21013_CLK_MODE_DIV 0x01
  72. #define BU21013_CLK_MODE_EXT 0x02
  73. #define BU21013_CLK_MODE_CALIB 0x80
  74. #define BU21013_IDLET_0 0x01
  75. #define BU21013_IDLET_1 0x02
  76. #define BU21013_IDLET_2 0x04
  77. #define BU21013_IDLET_3 0x08
  78. #define BU21013_IDLE_INTERMIT_EN 0x10
  79. #define BU21013_DELTA_0_6 0x7F
  80. #define BU21013_FILTER_EN 0x80
  81. #define BU21013_INT_MODE_LEVEL 0x00
  82. #define BU21013_INT_MODE_EDGE 0x01
  83. #define BU21013_GAIN_0 0x01
  84. #define BU21013_GAIN_1 0x02
  85. #define BU21013_GAIN_2 0x04
  86. #define BU21013_OFFSET_MODE_DEFAULT 0x00
  87. #define BU21013_OFFSET_MODE_MOVE 0x01
  88. #define BU21013_OFFSET_MODE_DISABLE 0x02
  89. #define BU21013_TH_ON_0 0x01
  90. #define BU21013_TH_ON_1 0x02
  91. #define BU21013_TH_ON_2 0x04
  92. #define BU21013_TH_ON_3 0x08
  93. #define BU21013_TH_ON_4 0x10
  94. #define BU21013_TH_ON_5 0x20
  95. #define BU21013_TH_ON_6 0x40
  96. #define BU21013_TH_ON_7 0x80
  97. #define BU21013_TH_ON_MAX 0xFF
  98. #define BU21013_TH_OFF_0 0x01
  99. #define BU21013_TH_OFF_1 0x02
  100. #define BU21013_TH_OFF_2 0x04
  101. #define BU21013_TH_OFF_3 0x08
  102. #define BU21013_TH_OFF_4 0x10
  103. #define BU21013_TH_OFF_5 0x20
  104. #define BU21013_TH_OFF_6 0x40
  105. #define BU21013_TH_OFF_7 0x80
  106. #define BU21013_TH_OFF_MAX 0xFF
  107. #define BU21013_X_EDGE_0 0x01
  108. #define BU21013_X_EDGE_1 0x02
  109. #define BU21013_X_EDGE_2 0x04
  110. #define BU21013_X_EDGE_3 0x08
  111. #define BU21013_Y_EDGE_0 0x10
  112. #define BU21013_Y_EDGE_1 0x20
  113. #define BU21013_Y_EDGE_2 0x40
  114. #define BU21013_Y_EDGE_3 0x80
  115. #define BU21013_DONE 0x01
  116. #define BU21013_NUMBER_OF_X_SENSORS (6)
  117. #define BU21013_NUMBER_OF_Y_SENSORS (11)
  118. #define DRIVER_TP "bu21013_tp"
  119. /**
  120. * struct bu21013_ts_data - touch panel data structure
  121. * @client: pointer to the i2c client
  122. * @wait: variable to wait_queue_head_t structure
  123. * @touch_stopped: touch stop flag
  124. * @chip: pointer to the touch panel controller
  125. * @in_dev: pointer to the input device structure
  126. * @intr_pin: interrupt pin value
  127. * @regulator: pointer to the Regulator used for touch screen
  128. *
  129. * Touch panel device data structure
  130. */
  131. struct bu21013_ts_data {
  132. struct i2c_client *client;
  133. wait_queue_head_t wait;
  134. const struct bu21013_platform_device *chip;
  135. struct input_dev *in_dev;
  136. struct regulator *regulator;
  137. unsigned int irq;
  138. unsigned int intr_pin;
  139. bool touch_stopped;
  140. };
  141. /**
  142. * bu21013_read_block_data(): read the touch co-ordinates
  143. * @data: bu21013_ts_data structure pointer
  144. * @buf: byte pointer
  145. *
  146. * Read the touch co-ordinates using i2c read block into buffer
  147. * and returns integer.
  148. */
  149. static int bu21013_read_block_data(struct bu21013_ts_data *data, u8 *buf)
  150. {
  151. int ret, i;
  152. for (i = 0; i < I2C_RETRY_COUNT; i++) {
  153. ret = i2c_smbus_read_i2c_block_data
  154. (data->client, BU21013_SENSORS_BTN_0_7_REG,
  155. LENGTH_OF_BUFFER, buf);
  156. if (ret == LENGTH_OF_BUFFER)
  157. return 0;
  158. }
  159. return -EINVAL;
  160. }
  161. /**
  162. * bu21013_do_touch_report(): Get the touch co-ordinates
  163. * @data: bu21013_ts_data structure pointer
  164. *
  165. * Get the touch co-ordinates from touch sensor registers and writes
  166. * into device structure and returns integer.
  167. */
  168. static int bu21013_do_touch_report(struct bu21013_ts_data *data)
  169. {
  170. u8 buf[LENGTH_OF_BUFFER];
  171. unsigned int pos_x[2], pos_y[2];
  172. bool has_x_sensors, has_y_sensors;
  173. int finger_down_count = 0;
  174. int i;
  175. if (data == NULL)
  176. return -EINVAL;
  177. if (bu21013_read_block_data(data, buf) < 0)
  178. return -EINVAL;
  179. has_x_sensors = hweight32(buf[0] & BU21013_SENSORS_EN_0_7);
  180. has_y_sensors = hweight32(((buf[1] & BU21013_SENSORS_EN_8_15) |
  181. ((buf[2] & BU21013_SENSORS_EN_16_23) << SHIFT_8)) >> SHIFT_2);
  182. if (!has_x_sensors || !has_y_sensors)
  183. return 0;
  184. for (i = 0; i < MAX_FINGERS; i++) {
  185. const u8 *p = &buf[4 * i + 3];
  186. unsigned int x = p[0] << SHIFT_2 | (p[1] & MASK_BITS);
  187. unsigned int y = p[2] << SHIFT_2 | (p[3] & MASK_BITS);
  188. if (x == 0 || y == 0)
  189. continue;
  190. pos_x[finger_down_count] = x;
  191. pos_y[finger_down_count] = y;
  192. finger_down_count++;
  193. }
  194. if (finger_down_count) {
  195. if (finger_down_count == 2 &&
  196. (abs(pos_x[0] - pos_x[1]) < DELTA_MIN ||
  197. abs(pos_y[0] - pos_y[1]) < DELTA_MIN)) {
  198. return 0;
  199. }
  200. for (i = 0; i < finger_down_count; i++) {
  201. if (data->chip->x_flip)
  202. pos_x[i] = data->chip->touch_x_max - pos_x[i];
  203. if (data->chip->y_flip)
  204. pos_y[i] = data->chip->touch_y_max - pos_y[i];
  205. input_report_abs(data->in_dev,
  206. ABS_MT_POSITION_X, pos_x[i]);
  207. input_report_abs(data->in_dev,
  208. ABS_MT_POSITION_Y, pos_y[i]);
  209. input_mt_sync(data->in_dev);
  210. }
  211. } else
  212. input_mt_sync(data->in_dev);
  213. input_sync(data->in_dev);
  214. return 0;
  215. }
  216. /**
  217. * bu21013_gpio_irq() - gpio thread function for touch interrupt
  218. * @irq: irq value
  219. * @device_data: void pointer
  220. *
  221. * This gpio thread function for touch interrupt
  222. * and returns irqreturn_t.
  223. */
  224. static irqreturn_t bu21013_gpio_irq(int irq, void *device_data)
  225. {
  226. struct bu21013_ts_data *data = device_data;
  227. struct i2c_client *i2c = data->client;
  228. int retval;
  229. do {
  230. retval = bu21013_do_touch_report(data);
  231. if (retval < 0) {
  232. dev_err(&i2c->dev, "bu21013_do_touch_report failed\n");
  233. return IRQ_NONE;
  234. }
  235. data->intr_pin = gpio_get_value(data->chip->touch_pin);
  236. if (data->intr_pin == PEN_DOWN_INTR)
  237. wait_event_timeout(data->wait, data->touch_stopped,
  238. msecs_to_jiffies(2));
  239. } while (!data->intr_pin && !data->touch_stopped);
  240. return IRQ_HANDLED;
  241. }
  242. /**
  243. * bu21013_init_chip() - power on sequence for the bu21013 controller
  244. * @data: device structure pointer
  245. *
  246. * This function is used to power on
  247. * the bu21013 controller and returns integer.
  248. */
  249. static int bu21013_init_chip(struct bu21013_ts_data *data)
  250. {
  251. int retval;
  252. struct i2c_client *i2c = data->client;
  253. retval = i2c_smbus_write_byte_data(i2c, BU21013_RESET_REG,
  254. BU21013_RESET_ENABLE);
  255. if (retval < 0) {
  256. dev_err(&i2c->dev, "BU21013_RESET reg write failed\n");
  257. return retval;
  258. }
  259. msleep(RESET_DELAY);
  260. retval = i2c_smbus_write_byte_data(i2c, BU21013_SENSOR_0_7_REG,
  261. BU21013_SENSORS_EN_0_7);
  262. if (retval < 0) {
  263. dev_err(&i2c->dev, "BU21013_SENSOR_0_7 reg write failed\n");
  264. return retval;
  265. }
  266. retval = i2c_smbus_write_byte_data(i2c, BU21013_SENSOR_8_15_REG,
  267. BU21013_SENSORS_EN_8_15);
  268. if (retval < 0) {
  269. dev_err(&i2c->dev, "BU21013_SENSOR_8_15 reg write failed\n");
  270. return retval;
  271. }
  272. retval = i2c_smbus_write_byte_data(i2c, BU21013_SENSOR_16_23_REG,
  273. BU21013_SENSORS_EN_16_23);
  274. if (retval < 0) {
  275. dev_err(&i2c->dev, "BU21013_SENSOR_16_23 reg write failed\n");
  276. return retval;
  277. }
  278. retval = i2c_smbus_write_byte_data(i2c, BU21013_POS_MODE1_REG,
  279. (BU21013_POS_MODE1_0 | BU21013_POS_MODE1_1));
  280. if (retval < 0) {
  281. dev_err(&i2c->dev, "BU21013_POS_MODE1 reg write failed\n");
  282. return retval;
  283. }
  284. retval = i2c_smbus_write_byte_data(i2c, BU21013_POS_MODE2_REG,
  285. (BU21013_POS_MODE2_ZERO | BU21013_POS_MODE2_AVG1 |
  286. BU21013_POS_MODE2_AVG2 | BU21013_POS_MODE2_EN_RAW |
  287. BU21013_POS_MODE2_MULTI));
  288. if (retval < 0) {
  289. dev_err(&i2c->dev, "BU21013_POS_MODE2 reg write failed\n");
  290. return retval;
  291. }
  292. if (data->chip->ext_clk)
  293. retval = i2c_smbus_write_byte_data(i2c, BU21013_CLK_MODE_REG,
  294. (BU21013_CLK_MODE_EXT | BU21013_CLK_MODE_CALIB));
  295. else
  296. retval = i2c_smbus_write_byte_data(i2c, BU21013_CLK_MODE_REG,
  297. (BU21013_CLK_MODE_DIV | BU21013_CLK_MODE_CALIB));
  298. if (retval < 0) {
  299. dev_err(&i2c->dev, "BU21013_CLK_MODE reg write failed\n");
  300. return retval;
  301. }
  302. retval = i2c_smbus_write_byte_data(i2c, BU21013_IDLE_REG,
  303. (BU21013_IDLET_0 | BU21013_IDLE_INTERMIT_EN));
  304. if (retval < 0) {
  305. dev_err(&i2c->dev, "BU21013_IDLE reg write failed\n");
  306. return retval;
  307. }
  308. retval = i2c_smbus_write_byte_data(i2c, BU21013_INT_MODE_REG,
  309. BU21013_INT_MODE_LEVEL);
  310. if (retval < 0) {
  311. dev_err(&i2c->dev, "BU21013_INT_MODE reg write failed\n");
  312. return retval;
  313. }
  314. retval = i2c_smbus_write_byte_data(i2c, BU21013_FILTER_REG,
  315. (BU21013_DELTA_0_6 |
  316. BU21013_FILTER_EN));
  317. if (retval < 0) {
  318. dev_err(&i2c->dev, "BU21013_FILTER reg write failed\n");
  319. return retval;
  320. }
  321. retval = i2c_smbus_write_byte_data(i2c, BU21013_TH_ON_REG,
  322. BU21013_TH_ON_5);
  323. if (retval < 0) {
  324. dev_err(&i2c->dev, "BU21013_TH_ON reg write failed\n");
  325. return retval;
  326. }
  327. retval = i2c_smbus_write_byte_data(i2c, BU21013_TH_OFF_REG,
  328. BU21013_TH_OFF_4 | BU21013_TH_OFF_3);
  329. if (retval < 0) {
  330. dev_err(&i2c->dev, "BU21013_TH_OFF reg write failed\n");
  331. return retval;
  332. }
  333. retval = i2c_smbus_write_byte_data(i2c, BU21013_GAIN_REG,
  334. (BU21013_GAIN_0 | BU21013_GAIN_1));
  335. if (retval < 0) {
  336. dev_err(&i2c->dev, "BU21013_GAIN reg write failed\n");
  337. return retval;
  338. }
  339. retval = i2c_smbus_write_byte_data(i2c, BU21013_OFFSET_MODE_REG,
  340. BU21013_OFFSET_MODE_DEFAULT);
  341. if (retval < 0) {
  342. dev_err(&i2c->dev, "BU21013_OFFSET_MODE reg write failed\n");
  343. return retval;
  344. }
  345. retval = i2c_smbus_write_byte_data(i2c, BU21013_XY_EDGE_REG,
  346. (BU21013_X_EDGE_0 | BU21013_X_EDGE_2 |
  347. BU21013_Y_EDGE_1 | BU21013_Y_EDGE_3));
  348. if (retval < 0) {
  349. dev_err(&i2c->dev, "BU21013_XY_EDGE reg write failed\n");
  350. return retval;
  351. }
  352. retval = i2c_smbus_write_byte_data(i2c, BU21013_DONE_REG,
  353. BU21013_DONE);
  354. if (retval < 0) {
  355. dev_err(&i2c->dev, "BU21013_REG_DONE reg write failed\n");
  356. return retval;
  357. }
  358. return 0;
  359. }
  360. /**
  361. * bu21013_free_irq() - frees IRQ registered for touchscreen
  362. * @bu21013_data: device structure pointer
  363. *
  364. * This function signals interrupt thread to stop processing and
  365. * frees interrupt.
  366. */
  367. static void bu21013_free_irq(struct bu21013_ts_data *bu21013_data)
  368. {
  369. bu21013_data->touch_stopped = true;
  370. wake_up(&bu21013_data->wait);
  371. free_irq(bu21013_data->irq, bu21013_data);
  372. }
  373. /**
  374. * bu21013_cs_disable() - deconfigures the touch panel controller
  375. * @bu21013_data: device structure pointer
  376. *
  377. * This function is used to deconfigure the chip selection
  378. * for touch panel controller.
  379. */
  380. static void bu21013_cs_disable(struct bu21013_ts_data *bu21013_data)
  381. {
  382. int error;
  383. error = gpio_direction_output(bu21013_data->chip->cs_pin, 0);
  384. if (error < 0)
  385. dev_warn(&bu21013_data->client->dev,
  386. "%s: gpio direction failed, error: %d\n",
  387. __func__, error);
  388. else
  389. gpio_set_value(bu21013_data->chip->cs_pin, 0);
  390. gpio_free(bu21013_data->chip->cs_pin);
  391. }
  392. #ifdef CONFIG_OF
  393. static const struct bu21013_platform_device *
  394. bu21013_parse_dt(struct device *dev)
  395. {
  396. struct device_node *np = dev->of_node;
  397. struct bu21013_platform_device *pdata;
  398. if (!np) {
  399. dev_err(dev, "no device tree or platform data\n");
  400. return ERR_PTR(-EINVAL);
  401. }
  402. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  403. if (!pdata)
  404. return ERR_PTR(-ENOMEM);
  405. pdata->y_flip = pdata->x_flip = false;
  406. pdata->x_flip = of_property_read_bool(np, "rohm,flip-x");
  407. pdata->y_flip = of_property_read_bool(np, "rohm,flip-y");
  408. of_property_read_u32(np, "rohm,touch-max-x", &pdata->touch_x_max);
  409. of_property_read_u32(np, "rohm,touch-max-y", &pdata->touch_y_max);
  410. pdata->touch_pin = of_get_named_gpio(np, "touch-gpio", 0);
  411. pdata->cs_pin = of_get_named_gpio(np, "reset-gpio", 0);
  412. pdata->ext_clk = false;
  413. return pdata;
  414. }
  415. #else
  416. static inline const struct bu21013_platform_device *
  417. bu21013_parse_dt(struct device *dev)
  418. {
  419. dev_err(dev, "no platform data available\n");
  420. return ERR_PTR(-EINVAL);
  421. }
  422. #endif
  423. /**
  424. * bu21013_probe() - initializes the i2c-client touchscreen driver
  425. * @client: i2c client structure pointer
  426. * @id: i2c device id pointer
  427. *
  428. * This function used to initializes the i2c-client touchscreen
  429. * driver and returns integer.
  430. */
  431. static int bu21013_probe(struct i2c_client *client,
  432. const struct i2c_device_id *id)
  433. {
  434. const struct bu21013_platform_device *pdata =
  435. dev_get_platdata(&client->dev);
  436. struct bu21013_ts_data *bu21013_data;
  437. struct input_dev *in_dev;
  438. int error;
  439. if (!i2c_check_functionality(client->adapter,
  440. I2C_FUNC_SMBUS_BYTE_DATA)) {
  441. dev_err(&client->dev, "i2c smbus byte data not supported\n");
  442. return -EIO;
  443. }
  444. if (!pdata) {
  445. pdata = bu21013_parse_dt(&client->dev);
  446. if (IS_ERR(pdata))
  447. return PTR_ERR(pdata);
  448. }
  449. if (!gpio_is_valid(pdata->touch_pin)) {
  450. dev_err(&client->dev, "invalid touch_pin supplied\n");
  451. return -EINVAL;
  452. }
  453. bu21013_data = kzalloc(sizeof(struct bu21013_ts_data), GFP_KERNEL);
  454. in_dev = input_allocate_device();
  455. if (!bu21013_data || !in_dev) {
  456. dev_err(&client->dev, "device memory alloc failed\n");
  457. error = -ENOMEM;
  458. goto err_free_mem;
  459. }
  460. bu21013_data->in_dev = in_dev;
  461. bu21013_data->chip = pdata;
  462. bu21013_data->client = client;
  463. bu21013_data->irq = gpio_to_irq(pdata->touch_pin);
  464. bu21013_data->regulator = regulator_get(&client->dev, "avdd");
  465. if (IS_ERR(bu21013_data->regulator)) {
  466. dev_err(&client->dev, "regulator_get failed\n");
  467. error = PTR_ERR(bu21013_data->regulator);
  468. goto err_free_mem;
  469. }
  470. error = regulator_enable(bu21013_data->regulator);
  471. if (error < 0) {
  472. dev_err(&client->dev, "regulator enable failed\n");
  473. goto err_put_regulator;
  474. }
  475. bu21013_data->touch_stopped = false;
  476. init_waitqueue_head(&bu21013_data->wait);
  477. /* configure the gpio pins */
  478. error = gpio_request_one(pdata->cs_pin, GPIOF_OUT_INIT_HIGH,
  479. "touchp_reset");
  480. if (error < 0) {
  481. dev_err(&client->dev, "Unable to request gpio reset_pin\n");
  482. goto err_disable_regulator;
  483. }
  484. /* configure the touch panel controller */
  485. error = bu21013_init_chip(bu21013_data);
  486. if (error) {
  487. dev_err(&client->dev, "error in bu21013 config\n");
  488. goto err_cs_disable;
  489. }
  490. /* register the device to input subsystem */
  491. in_dev->name = DRIVER_TP;
  492. in_dev->id.bustype = BUS_I2C;
  493. in_dev->dev.parent = &client->dev;
  494. __set_bit(EV_SYN, in_dev->evbit);
  495. __set_bit(EV_KEY, in_dev->evbit);
  496. __set_bit(EV_ABS, in_dev->evbit);
  497. input_set_abs_params(in_dev, ABS_MT_POSITION_X, 0,
  498. pdata->touch_x_max, 0, 0);
  499. input_set_abs_params(in_dev, ABS_MT_POSITION_Y, 0,
  500. pdata->touch_y_max, 0, 0);
  501. input_set_drvdata(in_dev, bu21013_data);
  502. error = request_threaded_irq(bu21013_data->irq, NULL, bu21013_gpio_irq,
  503. IRQF_TRIGGER_FALLING | IRQF_SHARED |
  504. IRQF_ONESHOT,
  505. DRIVER_TP, bu21013_data);
  506. if (error) {
  507. dev_err(&client->dev, "request irq %d failed\n",
  508. bu21013_data->irq);
  509. goto err_cs_disable;
  510. }
  511. error = input_register_device(in_dev);
  512. if (error) {
  513. dev_err(&client->dev, "failed to register input device\n");
  514. goto err_free_irq;
  515. }
  516. device_init_wakeup(&client->dev, pdata->wakeup);
  517. i2c_set_clientdata(client, bu21013_data);
  518. return 0;
  519. err_free_irq:
  520. bu21013_free_irq(bu21013_data);
  521. err_cs_disable:
  522. bu21013_cs_disable(bu21013_data);
  523. err_disable_regulator:
  524. regulator_disable(bu21013_data->regulator);
  525. err_put_regulator:
  526. regulator_put(bu21013_data->regulator);
  527. err_free_mem:
  528. input_free_device(in_dev);
  529. kfree(bu21013_data);
  530. return error;
  531. }
  532. /**
  533. * bu21013_remove() - removes the i2c-client touchscreen driver
  534. * @client: i2c client structure pointer
  535. *
  536. * This function uses to remove the i2c-client
  537. * touchscreen driver and returns integer.
  538. */
  539. static int bu21013_remove(struct i2c_client *client)
  540. {
  541. struct bu21013_ts_data *bu21013_data = i2c_get_clientdata(client);
  542. bu21013_free_irq(bu21013_data);
  543. bu21013_cs_disable(bu21013_data);
  544. input_unregister_device(bu21013_data->in_dev);
  545. regulator_disable(bu21013_data->regulator);
  546. regulator_put(bu21013_data->regulator);
  547. kfree(bu21013_data);
  548. device_init_wakeup(&client->dev, false);
  549. return 0;
  550. }
  551. #ifdef CONFIG_PM
  552. /**
  553. * bu21013_suspend() - suspend the touch screen controller
  554. * @dev: pointer to device structure
  555. *
  556. * This function is used to suspend the
  557. * touch panel controller and returns integer
  558. */
  559. static int bu21013_suspend(struct device *dev)
  560. {
  561. struct bu21013_ts_data *bu21013_data = dev_get_drvdata(dev);
  562. struct i2c_client *client = bu21013_data->client;
  563. bu21013_data->touch_stopped = true;
  564. if (device_may_wakeup(&client->dev))
  565. enable_irq_wake(bu21013_data->irq);
  566. else
  567. disable_irq(bu21013_data->irq);
  568. regulator_disable(bu21013_data->regulator);
  569. return 0;
  570. }
  571. /**
  572. * bu21013_resume() - resume the touch screen controller
  573. * @dev: pointer to device structure
  574. *
  575. * This function is used to resume the touch panel
  576. * controller and returns integer.
  577. */
  578. static int bu21013_resume(struct device *dev)
  579. {
  580. struct bu21013_ts_data *bu21013_data = dev_get_drvdata(dev);
  581. struct i2c_client *client = bu21013_data->client;
  582. int retval;
  583. retval = regulator_enable(bu21013_data->regulator);
  584. if (retval < 0) {
  585. dev_err(&client->dev, "bu21013 regulator enable failed\n");
  586. return retval;
  587. }
  588. retval = bu21013_init_chip(bu21013_data);
  589. if (retval < 0) {
  590. dev_err(&client->dev, "bu21013 controller config failed\n");
  591. return retval;
  592. }
  593. bu21013_data->touch_stopped = false;
  594. if (device_may_wakeup(&client->dev))
  595. disable_irq_wake(bu21013_data->irq);
  596. else
  597. enable_irq(bu21013_data->irq);
  598. return 0;
  599. }
  600. static const struct dev_pm_ops bu21013_dev_pm_ops = {
  601. .suspend = bu21013_suspend,
  602. .resume = bu21013_resume,
  603. };
  604. #endif
  605. static const struct i2c_device_id bu21013_id[] = {
  606. { DRIVER_TP, 0 },
  607. { }
  608. };
  609. MODULE_DEVICE_TABLE(i2c, bu21013_id);
  610. static struct i2c_driver bu21013_driver = {
  611. .driver = {
  612. .name = DRIVER_TP,
  613. #ifdef CONFIG_PM
  614. .pm = &bu21013_dev_pm_ops,
  615. #endif
  616. },
  617. .probe = bu21013_probe,
  618. .remove = bu21013_remove,
  619. .id_table = bu21013_id,
  620. };
  621. module_i2c_driver(bu21013_driver);
  622. MODULE_LICENSE("GPL v2");
  623. MODULE_AUTHOR("Naveen Kumar G <naveen.gaddipati@stericsson.com>");
  624. MODULE_DESCRIPTION("bu21013 touch screen controller driver");