xref: /OK3568_Linux_fs/kernel/drivers/gpu/drm/rockchip/ebc-dev/pmic/tps65185.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2020 Rockchip Electronics Co. Ltd.
4  *
5  * Author: Zorro Liu <zorro.liu@rock-chips.com>
6  */
7 
8 #include <linux/device.h>
9 #include <linux/of_gpio.h>
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/errno.h>
13 #include <linux/string.h>
14 #include <linux/mm.h>
15 #include <linux/slab.h>
16 #include <linux/delay.h>
17 #include <linux/device.h>
18 #include <linux/major.h>
19 #include <linux/fs.h>
20 #include <linux/cdev.h>
21 #include <linux/uaccess.h>
22 #include <linux/interrupt.h>
23 #include <linux/workqueue.h>
24 #include <linux/delay.h>
25 #include <linux/i2c.h>
26 #include <linux/version.h>
27 #include <linux/suspend.h>
28 #include <linux/soc/rockchip/rk_vendor_storage.h>
29 #include <linux/gpio.h>
30 #include <linux/gpio/consumer.h>
31 #include "ebc_pmic.h"
32 
33 /* After waking up from sleep, Papyrus
34    waits for VN to be discharged and all
35    voltage ref to startup before loading
36    the default EEPROM settings. So accessing
37    registers too early after WAKEUP could
38    cause the register to be overridden by
39    default values */
40 #define PAPYRUS_EEPROM_DELAY_MS 50
41 /* Papyrus WAKEUP pin must stay low for
42    a minimum time */
43 #define PAPYRUS_SLEEP_MINIMUM_MS 110
44 /* Temp sensor might take a little time to
45    settle eventhough the status bit in TMST1
46    state conversion is done - if read too early
47    0C will be returned instead of the right temp */
48 #define PAPYRUS_TEMP_READ_TIME_MS 10
49 /* Powerup sequence takes at least 24 ms - no need to poll too frequently */
50 #define HW_GET_STATE_INTERVAL_MS 24
51 
52 #define SEQ_VDD(index)		((index % 4) << 6)
53 #define SEQ_VPOS(index)	((index % 4) << 4)
54 #define SEQ_VEE(index)		((index % 4) << 2)
55 #define SEQ_VNEG(index)	((index % 4) << 0)
56 
57 /* power up seq delay time */
58 #define UDLY_3ms(index)	(0x00 << ((index%4) * 2))
59 #define UDLY_6ms(index)	(0x01 << ((index%4) * 2))
60 #define UDLY_9ms(index)	(0x10 << ((index%4) * 2))
61 #define UDLY_12ms(index)	(0x11 << ((index%4) * 2))
62 
63 /* power down seq delay time */
64 #define DDLY_6ms(index)	(0x00 << ((index%4) * 2))
65 #define DDLY_12ms(index)	(0x01 << ((index%4) * 2))
66 #define DDLY_24ms(index)	(0x10 << ((index%4) * 2))
67 #define DDLY_48ms(index)	(0x11 << ((index%4) * 2))
68 
69 #define NUMBER_PMIC_REGS	10
70 
71 #define PAPYRUS_ADDR_TMST_VALUE	0x00
72 #define PAPYRUS_ADDR_ENABLE			0x01
73 #define PAPYRUS_ADDR_VADJ			0x02
74 #define PAPYRUS_ADDR_VCOM1_ADJUST	0x03
75 #define PAPYRUS_ADDR_VCOM2_ADJUST	0x04
76 #define PAPYRUS_ADDR_INT_ENABLE1	0x05
77 #define PAPYRUS_ADDR_INT_ENABLE2	0x06
78 #define PAPYRUS_ADDR_INT_STATUS1	0x07
79 #define PAPYRUS_ADDR_INT_STATUS2	0x08
80 #define PAPYRUS_ADDR_UPSEQ0			0x09
81 #define PAPYRUS_ADDR_UPSEQ1			0x0a
82 #define PAPYRUS_ADDR_DWNSEQ0		0x0b
83 #define PAPYRUS_ADDR_DWNSEQ1		0x0c
84 #define PAPYRUS_ADDR_TMST1			0x0d
85 #define PAPYRUS_ADDR_TMST2			0x0e
86 #define PAPYRUS_ADDR_PG_STATUS		0x0f
87 #define PAPYRUS_ADDR_REVID			0x10
88 
89 // INT_ENABLE1
90 #define PAPYRUS_INT_ENABLE1_ACQC_EN	1
91 #define PAPYRUS_INT_ENABLE1_PRGC_EN		0
92 
93 // INT_STATUS1
94 #define PAPYRUS_INT_STATUS1_ACQC		1
95 #define PAPYRUS_INT_STATUS1_PRGC		0
96 
97 // VCOM2_ADJUST
98 #define PAPYRUS_VCOM2_ACQ		7
99 #define PAPYRUS_VCOM2_PROG		6
100 #define PAPYRUS_VCOM2_HIZ		5
101 
102 #define PAPYRUS_MV_TO_VCOMREG(MV)	((MV) / 10)
103 
104 #define V3P3_EN_MASK		0x20
105 #define PAPYRUS_V3P3OFF_DELAY_MS	20//100
106 
107 struct papyrus_sess {
108 	struct device *dev;
109 	struct i2c_client *client;
110 	uint8_t enable_reg_shadow;
111 	uint8_t vadj;
112 	uint8_t vcom1;
113 	uint8_t vcom2;
114 	uint8_t upseq0;
115 	uint8_t upseq1;
116 	uint8_t dwnseq0;
117 	uint8_t dwnseq1;
118 	int irq;
119 	struct gpio_desc *int_pin;
120 	struct gpio_desc *pwr_en_pin;
121 	struct gpio_desc *pwr_up_pin;
122 	struct gpio_desc *wake_up_pin;
123 	struct gpio_desc *vcom_ctl_pin;
124 	struct mutex power_lock;
125 	struct workqueue_struct *tmp_monitor_wq;
126 	struct delayed_work tmp_delay_work;
127 };
128 
129 struct papyrus_hw_state {
130 	uint8_t tmst_value;
131 	uint8_t int_status1;
132 	uint8_t int_status2;
133 	uint8_t pg_status;
134 };
135 static bool papyrus_need_reconfig = true;
136 
papyrus_hw_setreg(struct papyrus_sess * sess,uint8_t regaddr,uint8_t val)137 static int papyrus_hw_setreg(struct papyrus_sess *sess, uint8_t regaddr, uint8_t val)
138 {
139 	int stat;
140 	uint8_t txbuf[2] = { regaddr, val };
141 	struct i2c_msg msgs[] = {
142 		{
143 			.addr = sess->client->addr,
144 			.flags = 0,
145 			.len = 2,
146 			.buf = txbuf,
147 		}
148 	};
149 
150 	stat = i2c_transfer(sess->client->adapter, msgs, ARRAY_SIZE(msgs));
151 
152 	if (stat < 0) {
153 		dev_err(&sess->client->dev, "i2c send error: %d\n", stat);
154 	} else if (stat != ARRAY_SIZE(msgs)) {
155 		dev_err(&sess->client->dev, "i2c send N mismatch: %d\n", stat);
156 		stat = -EIO;
157 	} else {
158 		stat = 0;
159 	}
160 
161 	return stat;
162 }
163 
papyrus_hw_getreg(struct papyrus_sess * sess,uint8_t regaddr,uint8_t * val)164 static int papyrus_hw_getreg(struct papyrus_sess *sess, uint8_t regaddr, uint8_t *val)
165 {
166 	int stat;
167 	struct i2c_msg msgs[] = {
168 		{
169 			.addr = sess->client->addr,
170 			.flags = 0,
171 			.len = 1,
172 			.buf = &regaddr,
173 		},
174 		{
175 			.addr = sess->client->addr,
176 			.flags = I2C_M_RD,
177 			.len = 1,
178 			.buf = val,
179 		}
180 	};
181 
182 	stat = i2c_transfer(sess->client->adapter, msgs, ARRAY_SIZE(msgs));
183 	if (stat < 0) {
184 		dev_err(&sess->client->dev, "i2c read error: %d\n", stat);
185 	} else if (stat != ARRAY_SIZE(msgs)) {
186 		dev_err(&sess->client->dev, "i2c read N mismatch: %d\n", stat);
187 		stat = -EIO;
188 	} else {
189 		stat = 0;
190 	}
191 
192 	return stat;
193 }
194 
papyrus_hw_get_int_state(struct papyrus_sess * sess,struct papyrus_hw_state * hwst)195 static void papyrus_hw_get_int_state(struct papyrus_sess *sess, struct papyrus_hw_state *hwst)
196 {
197 	int stat;
198 
199 	stat = papyrus_hw_getreg(sess, PAPYRUS_ADDR_INT_STATUS1, &hwst->int_status1);
200 	if (stat)
201 		dev_err(&sess->client->dev, "i2c error: %d\n", stat);
202 
203 	stat = papyrus_hw_getreg(sess, PAPYRUS_ADDR_INT_STATUS2, &hwst->int_status2);
204 	if (stat)
205 		dev_err(&sess->client->dev, "i2c error: %d\n", stat);
206 }
207 #if 0
208 static bool papyrus_hw_power_ack(struct papyrus_sess *sess, int up)
209 {
210 	struct papyrus_hw_state hwst;
211 	int st;
212 	int retries_left = 10;
213 
214 	if ((up & ~(1UL))) {
215 		dev_err(&sess->client->dev, "invalid power flag %d.\n", up);
216 		return false;
217 	}
218 
219 	do {
220 		papyrus_hw_getreg(sess, PAPYRUS_ADDR_PG_STATUS, &hwst.pg_status);
221 		dev_dbg(&sess->client->dev, "hwst: tmst_val=%d, ist1=%02x, ist2=%02x, pg=%02x\n",
222 			hwst.tmst_value, hwst.int_status1, hwst.int_status2, hwst.pg_status);
223 		hwst.pg_status &= 0xfa;
224 		if ((hwst.pg_status == 0xfa) && (up == 1)) {
225 			st = 1;
226 		} else if ((hwst.pg_status == 0x00) && (up == 0)) {
227 			st = 0;
228 		} else {
229 			st = -1;	/* not settled yet */
230 			msleep(HW_GET_STATE_INTERVAL_MS);
231 		}
232 		retries_left--;
233 	} while ((st == -1) && retries_left);
234 
235 	if (st == -1)
236 		dev_err(&sess->client->dev, "power %s settle error (PG = %02x)\n", up ? "up" : "down", hwst.pg_status);
237 
238 	return (st == up);
239 }
240 #endif
papyrus_hw_send_powerup(struct papyrus_sess * sess)241 static void papyrus_hw_send_powerup(struct papyrus_sess *sess)
242 {
243 	int stat = 0;
244 
245 	stat |= papyrus_hw_setreg(sess, PAPYRUS_ADDR_VADJ, sess->vadj);
246 
247 	stat |= papyrus_hw_setreg(sess, PAPYRUS_ADDR_UPSEQ0, sess->upseq0);
248 	stat |= papyrus_hw_setreg(sess, PAPYRUS_ADDR_UPSEQ1, sess->upseq1);
249 	stat |= papyrus_hw_setreg(sess, PAPYRUS_ADDR_DWNSEQ0, sess->dwnseq0);
250 	stat |= papyrus_hw_setreg(sess, PAPYRUS_ADDR_DWNSEQ1, sess->dwnseq1);
251 
252 	//stat |= papyrus_hw_setreg(sess, PAPYRUS_ADDR_VCOM1_ADJUST, sess->vcom1);
253 	//stat |= papyrus_hw_setreg(sess, PAPYRUS_ADDR_VCOM2_ADJUST, sess->vcom2);
254 
255 	sess->enable_reg_shadow |= V3P3_EN_MASK;
256 	stat |= papyrus_hw_setreg(sess, PAPYRUS_ADDR_ENABLE, sess->enable_reg_shadow);
257 	usleep_range(2 * 1000, 3 * 1000);
258 
259 	sess->enable_reg_shadow = (0x80 | 0x30 | 0x0F);
260 	stat |= papyrus_hw_setreg(sess, PAPYRUS_ADDR_ENABLE, sess->enable_reg_shadow);
261 	if (stat)
262 		dev_err(&sess->client->dev, "i2c error: %d\n", stat);
263 	if (!IS_ERR_OR_NULL(sess->pwr_up_pin))
264 		gpiod_direction_output(sess->pwr_up_pin, 1);
265 
266 	return;
267 }
268 
papyrus_hw_send_powerdown(struct papyrus_sess * sess)269 static void papyrus_hw_send_powerdown(struct papyrus_sess *sess)
270 {
271 	int stat = 0;
272 
273 	sess->enable_reg_shadow = (0x40 | 0x20 | 0x0F);
274 	stat = papyrus_hw_setreg(sess, PAPYRUS_ADDR_ENABLE, sess->enable_reg_shadow);
275 	usleep_range(2 * 1000, 3 * 1000);
276 	sess->enable_reg_shadow &= ~V3P3_EN_MASK;
277 	stat |= papyrus_hw_setreg(sess, PAPYRUS_ADDR_ENABLE, sess->enable_reg_shadow);
278 	if (stat)
279 		dev_err(&sess->client->dev, "i2c error: %d\n", stat);
280 
281 
282 	return;
283 }
284 
papyrus_irq(int irq,void * dev_id)285 static irqreturn_t papyrus_irq(int irq, void *dev_id)
286 {
287 	struct papyrus_sess *sess = dev_id;
288 	struct papyrus_hw_state hwst;
289 
290 	papyrus_hw_get_int_state(sess, &hwst);
291 	dev_info(&sess->client->dev, "%s: (INT1 = %02x, INT2 = %02x)\n", __func__,
292 						hwst.int_status1, hwst.int_status2);
293 	//reset pmic
294 	if ((hwst.int_status2 & 0xfa) || (hwst.int_status1 & 0x04)) {
295 		if (sess->enable_reg_shadow | V3P3_EN_MASK)
296 			papyrus_hw_setreg(sess, PAPYRUS_ADDR_ENABLE, sess->enable_reg_shadow);
297 	}
298 
299 	return IRQ_HANDLED;
300 }
301 
papyrus_hw_get_revid(struct papyrus_sess * sess)302 static int papyrus_hw_get_revid(struct papyrus_sess *sess)
303 {
304 	int stat;
305 	uint8_t revid;
306 
307 	stat = papyrus_hw_getreg(sess, PAPYRUS_ADDR_REVID, &revid);
308 	if (stat) {
309 		dev_err(&sess->client->dev, "i2c error: %d\n", stat);
310 		return stat;
311 	} else {
312 		return revid;
313 	}
314 }
315 
papyrus_hw_arg_init(struct papyrus_sess * sess)316 static void papyrus_hw_arg_init(struct papyrus_sess *sess)
317 {
318 	sess->vadj = 0x03;
319 	sess->upseq0 = SEQ_VEE(0) | SEQ_VNEG(1) | SEQ_VPOS(2) | SEQ_VDD(3);
320 	sess->upseq1 = UDLY_3ms(0) | UDLY_3ms(1) | UDLY_3ms(2) | UDLY_3ms(3);
321 	sess->dwnseq0 = SEQ_VDD(0) | SEQ_VPOS(1) | SEQ_VNEG(2) | SEQ_VEE(3);
322 	sess->dwnseq1 = DDLY_6ms(0) | DDLY_6ms(1) | DDLY_6ms(2) | DDLY_6ms(3);
323 	sess->vcom1 = (PAPYRUS_MV_TO_VCOMREG(1560) & 0x00FF);
324 	sess->vcom2 = ((PAPYRUS_MV_TO_VCOMREG(1560) & 0x0100) >> 8);
325 }
326 
papyrus_hw_init(struct papyrus_sess * sess)327 static int papyrus_hw_init(struct papyrus_sess *sess)
328 {
329 	int stat = 0;
330 
331 	if (!IS_ERR_OR_NULL(sess->pwr_en_pin)) {
332 		gpiod_direction_output(sess->pwr_en_pin, 1);
333 		usleep_range(2 * 1000, 3 * 1000);
334 	}
335 	gpiod_direction_output(sess->wake_up_pin, 0);
336 	/* wait to reset papyrus */
337 	msleep(PAPYRUS_SLEEP_MINIMUM_MS);
338 	gpiod_direction_output(sess->wake_up_pin, 1);
339 	/* power up pin no need to control, use i2c control */
340 	if (!IS_ERR_OR_NULL(sess->pwr_up_pin))
341 		gpiod_direction_output(sess->pwr_up_pin, 0);
342 	gpiod_direction_output(sess->vcom_ctl_pin, 1);
343 	msleep(PAPYRUS_EEPROM_DELAY_MS);
344 
345 	stat = papyrus_hw_get_revid(sess);
346 	if (stat < 0) {
347 		dev_err(&sess->client->dev, "get id failed");
348 		return stat;
349 	}
350 
351 	dev_info(&sess->client->dev, "detected device with ID=%02x (TPS6518%dr%dp%d)\n",
352 		 stat, stat & 0xF, (stat & 0xC0) >> 6, (stat & 0x30) >> 4);
353 
354 	return 0;
355 }
356 
papyrus_set_vcom_voltage(struct papyrus_sess * sess,int vcom_mv)357 static void papyrus_set_vcom_voltage(struct papyrus_sess *sess, int vcom_mv)
358 {
359 	sess->vcom1 = (PAPYRUS_MV_TO_VCOMREG(vcom_mv) & 0x00FF);
360 	sess->vcom2 = ((PAPYRUS_MV_TO_VCOMREG(vcom_mv) & 0x0100) >> 8);
361 }
362 
papyrus_hw_read_temperature(struct ebc_pmic * pmic,int * t)363 static int papyrus_hw_read_temperature(struct ebc_pmic *pmic, int *t)
364 {
365 	struct papyrus_sess *sess = (struct papyrus_sess *)pmic->drvpar;
366 	int stat;
367 	uint8_t tb;
368 #if 0
369 	int ntries = 50;
370 
371 	stat = papyrus_hw_setreg(sess, PAPYRUS_ADDR_TMST1, 0x80);
372 	if (stat)
373 		return stat;
374 	do {
375 		stat = papyrus_hw_getreg(sess, PAPYRUS_ADDR_TMST1, &tb);
376 	} while (!stat && ntries-- && (((tb & 0x20) == 0) || (tb & 0x80)));
377 
378 	if (stat)
379 		return stat;
380 
381 	msleep(PAPYRUS_TEMP_READ_TIME_MS);
382 #endif
383 	stat = papyrus_hw_getreg(sess, PAPYRUS_ADDR_TMST_VALUE, &tb);
384 	*t = (int)(int8_t)tb;
385 
386 	return stat;
387 }
388 
papyrus_hw_power_req(struct ebc_pmic * pmic,bool up)389 static void papyrus_hw_power_req(struct ebc_pmic *pmic, bool up)
390 {
391 	struct papyrus_sess *sess = (struct papyrus_sess *)pmic->drvpar;
392 
393 	if (up)
394 		mutex_lock(&sess->power_lock);
395 	if (papyrus_need_reconfig) {
396 		if (up) {
397 			papyrus_hw_send_powerup(sess);
398 			//papyrus_hw_power_ack(sess, up);
399 			enable_irq(sess->irq);
400 		} else {
401 			disable_irq(sess->irq);
402 			papyrus_hw_send_powerdown(sess);
403 			//papyrus_hw_power_ack(sess, up);
404 		}
405 		papyrus_need_reconfig = false;
406 	} else {
407 		if (up) {
408 			if (!IS_ERR_OR_NULL(sess->pwr_up_pin))
409 				gpiod_direction_output(sess->pwr_up_pin, 1);
410 			enable_irq(sess->irq);
411 		} else {
412 			disable_irq(sess->irq);
413 			if (!IS_ERR_OR_NULL(sess->pwr_up_pin))
414 				gpiod_direction_output(sess->pwr_up_pin, 0);
415 		}
416 	}
417 	if (!up)
418 		mutex_unlock(&sess->power_lock);
419 	return;
420 }
421 
papyrus_hw_vcom_get(struct ebc_pmic * pmic)422 static int papyrus_hw_vcom_get(struct ebc_pmic *pmic)
423 {
424 	struct papyrus_sess *sess = (struct papyrus_sess *)pmic->drvpar;
425 	uint8_t rev_val = 0;
426 	int stat = 0;
427 	int read_vcom_mv = 0;
428 
429 	mutex_lock(&sess->power_lock);
430 	// VERIFICATION
431 	gpiod_direction_output(sess->wake_up_pin, 0);
432 	msleep(10);
433 	gpiod_direction_output(sess->wake_up_pin, 1);
434 	msleep(10);
435 	read_vcom_mv = 0;
436 	stat |= papyrus_hw_getreg(sess, PAPYRUS_ADDR_VCOM1_ADJUST, &rev_val);
437 	read_vcom_mv += rev_val;
438 	stat |= papyrus_hw_getreg(sess, PAPYRUS_ADDR_VCOM2_ADJUST, &rev_val);
439 	read_vcom_mv += ((rev_val & 0x0001) << 8);
440 
441 	if (stat)
442 		dev_err(&sess->client->dev, "papyrus: I2C error: %d\n", stat);
443 	mutex_unlock(&sess->power_lock);
444 
445 	return read_vcom_mv * 10;
446 }
447 
papyrus_hw_vcom_set(struct ebc_pmic * pmic,int vcom_mv)448 static int papyrus_hw_vcom_set(struct ebc_pmic *pmic, int vcom_mv)
449 {
450 	struct papyrus_sess *sess = (struct papyrus_sess *)pmic->drvpar;
451 	uint8_t rev_val = 0;
452 	int stat = 0;
453 
454 	mutex_lock(&sess->power_lock);
455 	gpiod_direction_output(sess->wake_up_pin, 1);
456 	msleep(10);
457 	// Set vcom voltage
458 	papyrus_set_vcom_voltage(sess, vcom_mv);
459 	stat |= papyrus_hw_setreg(sess, PAPYRUS_ADDR_VCOM1_ADJUST, sess->vcom1);
460 	stat |= papyrus_hw_setreg(sess, PAPYRUS_ADDR_VCOM2_ADJUST, sess->vcom2);
461 
462 	// PROGRAMMING
463 	sess->vcom2 |= 1 << PAPYRUS_VCOM2_PROG;
464 	stat |= papyrus_hw_setreg(sess, PAPYRUS_ADDR_VCOM2_ADJUST, sess->vcom2);
465 	rev_val = 0;
466 	while (!(rev_val & (1 << PAPYRUS_INT_STATUS1_PRGC))) {
467 		stat |= papyrus_hw_getreg(sess, PAPYRUS_ADDR_INT_STATUS1, &rev_val);
468 		if (stat)
469 			break;
470 		msleep(50);
471 	}
472 
473 	if (stat)
474 		dev_err(&sess->client->dev, "papyrus: I2C error: %d\n", stat);
475 	mutex_unlock(&sess->power_lock);
476 
477 	return 0;
478 }
479 
papyrus_pm_sleep(struct ebc_pmic * pmic)480 static void papyrus_pm_sleep(struct ebc_pmic *pmic)
481 {
482 	struct papyrus_sess *s = (struct papyrus_sess *)pmic->drvpar;
483 
484 	cancel_delayed_work_sync(&s->tmp_delay_work);
485 
486 	mutex_lock(&s->power_lock);
487 	gpiod_direction_output(s->vcom_ctl_pin, 0);
488 	gpiod_direction_output(s->wake_up_pin, 0);
489 	if (!IS_ERR_OR_NULL(s->pwr_en_pin))
490 		gpiod_direction_output(s->pwr_en_pin, 0);
491 	papyrus_need_reconfig = true;
492 	mutex_unlock(&s->power_lock);
493 }
494 
papyrus_pm_resume(struct ebc_pmic * pmic)495 static void papyrus_pm_resume(struct ebc_pmic *pmic)
496 {
497 	struct papyrus_sess *s = (struct papyrus_sess *)pmic->drvpar;
498 
499 	mutex_lock(&s->power_lock);
500 	if (!IS_ERR_OR_NULL(s->pwr_en_pin)) {
501 		gpiod_direction_output(s->pwr_en_pin, 1);
502 		usleep_range(2 * 1000, 3 * 1000);
503 	}
504 	gpiod_direction_output(s->wake_up_pin, 1);
505 	gpiod_direction_output(s->vcom_ctl_pin, 1);
506 	usleep_range(2 * 1000, 3 * 1000);
507 	mutex_unlock(&s->power_lock);
508 
509 	//trigger temperature measurement
510 	papyrus_hw_setreg(s, PAPYRUS_ADDR_TMST1, 0x80);
511 	queue_delayed_work(s->tmp_monitor_wq, &s->tmp_delay_work,
512 			   msecs_to_jiffies(10000));
513 }
514 
papyrus_tmp_work(struct work_struct * work)515 static void papyrus_tmp_work(struct work_struct *work)
516 {
517 	struct papyrus_sess *s =
518 		container_of(work, struct papyrus_sess, tmp_delay_work.work);
519 
520 	//trigger temperature measurement
521 	papyrus_hw_setreg(s, PAPYRUS_ADDR_TMST1, 0x80);
522 
523 	queue_delayed_work(s->tmp_monitor_wq, &s->tmp_delay_work,
524 			   msecs_to_jiffies(10000));
525 }
526 
papyrus_probe(struct ebc_pmic * pmic,struct i2c_client * client)527 static int papyrus_probe(struct ebc_pmic *pmic, struct i2c_client *client)
528 {
529 	struct papyrus_sess *sess;
530 	int stat;
531 
532 	sess = devm_kzalloc(&client->dev, sizeof(*sess), GFP_KERNEL);
533 	if (!sess) {
534 		dev_err(&client->dev, "%s:%d: kzalloc failed\n", __func__, __LINE__);
535 		return -ENOMEM;
536 	}
537 	sess->client = client;
538 	mutex_init(&sess->power_lock);
539 	papyrus_hw_arg_init(sess);
540 
541 	sess->pwr_en_pin = devm_gpiod_get_optional(&client->dev, "poweren", GPIOD_OUT_HIGH);
542 	if (IS_ERR_OR_NULL(sess->pwr_en_pin)) {
543 		dev_err(&client->dev, "tsp65185: failed to find poweren pin, no defined\n");
544 	}
545 
546 	sess->wake_up_pin = devm_gpiod_get_optional(&client->dev, "wakeup", GPIOD_OUT_HIGH);
547 	if (IS_ERR_OR_NULL(sess->wake_up_pin)) {
548 		dev_err(&client->dev, "tsp65185: failed to find wakeup pin\n");
549 		return -ENOMEM;
550 	}
551 
552 	sess->vcom_ctl_pin = devm_gpiod_get_optional(&client->dev, "vcomctl", GPIOD_OUT_HIGH);
553 	if (IS_ERR_OR_NULL(sess->vcom_ctl_pin)) {
554 		dev_err(&client->dev, "tsp65185: failed to find vcom_ctl pin\n");
555 		return -ENOMEM;
556 	}
557 
558 	sess->pwr_up_pin = devm_gpiod_get_optional(&client->dev, "powerup", GPIOD_OUT_HIGH);
559 	if (IS_ERR_OR_NULL(sess->pwr_up_pin))
560 		dev_err(&client->dev, "tsp65185: no pwr_up pin find\n");
561 
562 	sess->int_pin = devm_gpiod_get(&client->dev, "int", GPIOD_IN);
563 	if (IS_ERR(sess->int_pin)) {
564 		dev_err(&client->dev, "tsp65185: failed to find int pin\n");
565 		return PTR_ERR(sess->int_pin);
566 	}
567 	sess->irq = gpiod_to_irq(sess->int_pin);
568 	if (sess->irq < 0) {
569 		dev_err(&client->dev, "Unable to get irq number for int pin\n");
570 		return sess->irq;
571 	}
572 
573 	irq_set_status_flags(sess->irq, IRQ_NOAUTOEN);
574 	stat = devm_request_threaded_irq(&client->dev, sess->irq, NULL, papyrus_irq,
575 					IRQF_TRIGGER_LOW | IRQF_ONESHOT,
576 					"tps65185", sess);
577 	if (stat) {
578 		dev_err(&client->dev,
579 			"Failed to enable IRQ, error: %d\n", stat);
580 		return stat;
581 	}
582 
583 	sess->tmp_monitor_wq = alloc_ordered_workqueue("%s",
584 			WQ_MEM_RECLAIM | WQ_FREEZABLE, "tps-tmp-monitor-wq");
585 	INIT_DELAYED_WORK(&sess->tmp_delay_work, papyrus_tmp_work);
586 	queue_delayed_work(sess->tmp_monitor_wq, &sess->tmp_delay_work,
587 			   msecs_to_jiffies(10000));
588 
589 	stat = papyrus_hw_init(sess);
590 	if (stat)
591 		return stat;
592 
593 	sess->enable_reg_shadow = 0;
594 	stat = papyrus_hw_setreg(sess, PAPYRUS_ADDR_ENABLE, sess->enable_reg_shadow);
595 	if (stat)
596 		return stat;
597 
598 	//trigger temperature measurement
599 	papyrus_hw_setreg(sess, PAPYRUS_ADDR_TMST1, 0x80);
600 
601 	pmic->drvpar = sess;
602 
603 	pmic->pmic_get_vcom = papyrus_hw_vcom_get;
604 	pmic->pmic_set_vcom = papyrus_hw_vcom_set;
605 	pmic->pmic_pm_resume = papyrus_pm_resume;
606 	pmic->pmic_pm_suspend = papyrus_pm_sleep;
607 	pmic->pmic_power_req = papyrus_hw_power_req;
608 	pmic->pmic_read_temperature = papyrus_hw_read_temperature;
609 
610 	return 0;
611 }
612 
tps65185_probe(struct i2c_client * client,const struct i2c_device_id * id)613 static int tps65185_probe(struct i2c_client *client, const struct i2c_device_id *id)
614 {
615 	struct ebc_pmic *pmic;
616 
617 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
618 		dev_err(&client->dev, "i2c check functionality failed.");
619 		return -ENODEV;
620 	}
621 
622 	pmic = devm_kzalloc(&client->dev, sizeof(*pmic), GFP_KERNEL);
623 	if (!pmic) {
624 		dev_err(&client->dev, "%s:%d: kzalloc failed\n", __func__, __LINE__);
625 		return -ENOMEM;
626 	}
627 
628 	if (0 != papyrus_probe(pmic, client)) {
629 		dev_err(&client->dev, "tps65185 hw init failed.");
630 		return -ENODEV;
631 	}
632 
633 	pmic->dev = &client->dev;
634 	sprintf(pmic->pmic_name, "tps65185");
635 	i2c_set_clientdata(client, pmic);
636 
637 	dev_info(&client->dev, "tps65185 probe ok.\n");
638 
639 	return 0;
640 }
641 
tps65185_remove(struct i2c_client * client)642 static int tps65185_remove(struct i2c_client *client)
643 {
644 	return 0;
645 }
646 
647 static const struct i2c_device_id tps65185_id[] = {
648 	{ "tps65185", 0 },
649 	{ }
650 };
651 
652 static const struct of_device_id tps65185_dt_ids[] = {
653 	{ .compatible = "ti,tps65185", },
654 	{ /* sentinel */ }
655 };
656 
657 MODULE_DEVICE_TABLE(of, tps65185_dt_ids);
658 static struct i2c_driver tps65185_driver = {
659 	.probe	= tps65185_probe,
660 	.remove 	= tps65185_remove,
661 	.id_table	= tps65185_id,
662 	.driver = {
663 		.of_match_table = tps65185_dt_ids,
664 		.name	  = "tps65185",
665 		.owner	  = THIS_MODULE,
666 	},
667 };
668 
669 module_i2c_driver(tps65185_driver);
670 
671 MODULE_DESCRIPTION("ti tps65185 pmic");
672 MODULE_LICENSE("GPL");
673