summaryrefslogtreecommitdiff
path: root/drivers/misc/sprd_dwnload/sprd_power_ctl.c
blob: 0b34dbe601f31eb02d1a9de8085b2d26aa44243c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/device.h>
#include <linux/miscdevice.h>
#include <linux/string.h>
#include <linux/major.h>
#include <linux/init.h>
#include <linux/kdev_t.h>
#include <asm/uaccess.h>
#include <linux/fs.h>
#include <linux/proc_fs.h>
#include <linux/timer.h>
#include <linux/wakelock.h>
#include <soc/sprd/gpio.h>
#include <linux/regulator/consumer.h>
#include <soc/sprd/regulator.h>
#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/earlysuspend.h>
#include <linux/wait.h>
#include <linux/poll.h>

#define	POWER_CTL_NAME	"power_ctl"

#define DOWNLOAD_IOCTL_BASE	'z'
#define DOWNLOAD_POWER_ON	_IO(DOWNLOAD_IOCTL_BASE, 0x01)
#define DOWNLOAD_POWER_OFF	_IO(DOWNLOAD_IOCTL_BASE, 0x02)
#define DOWNLOAD_POWER_RST	_IO(DOWNLOAD_IOCTL_BASE, 0x03)
#define GNSS_CHIP_EN		_IO(DOWNLOAD_IOCTL_BASE, 0x04)
#define GNSS_CHIP_DIS		_IO(DOWNLOAD_IOCTL_BASE, 0x05)
#define GNSS_LNA_EN		_IO(DOWNLOAD_IOCTL_BASE, 0x06)
#define GNSS_LNA_DIS		_IO(DOWNLOAD_IOCTL_BASE, 0x07)

struct sprd_gnss
{
	u32 chip_en;
	struct regulator *vdd_lna;
};

static struct sprd_gnss gnss_dev;

#ifdef CONFIG_HAS_EARLYSUSPEND
static struct early_suspend	gnss_suspend;
wait_queue_head_t	gnss_sleep_wait;
bool gnss_flag_sleep = false;
bool gnss_flag_resume = false;
static char gnss_status[16] = {0};
#endif


static int sprd_power_ctl_open(struct inode *inode,struct file *filp)
{
	return 0;
}

static int sprd_power_ctl_release(struct inode *inode,struct file *filp)
{
	return 0;
}


static void download_clk_init(bool enable)
{
	int err;
	const char *clk_name;
	struct device_node *np;
	static struct clk *download_clk = NULL;

	np = of_find_node_by_name(NULL, "sprd-marlin");
	if (!np) {
		printk(KERN_ERR"sprd-marlin not found");
		return;
	}

	err = of_property_read_string(np, "clk-name", &clk_name);
	if (err) {
		printk(KERN_ERR"get clk-name failed");
		return;
	}

	if (download_clk == NULL) {
		download_clk = clk_get(NULL, clk_name);
		if (IS_ERR(download_clk)) {
			printk("failed to get parent %s\n", clk_name);
			return;
		}
		clk_set_rate(download_clk, 32000);
	}

	#ifdef CONFIG_OF
	if (enable) {
		clk_prepare_enable(download_clk);
	}else{
		clk_disable_unprepare(download_clk);
	}
	#else
	if (enable) {
		clk_enable(download_clk);
	}else{
		clk_disable(download_clk);
	}
	#endif
}

static DEFINE_MUTEX(power_ctl_lock);
static void sprd_download_poweron(bool enable)
{
	int ret;
	int gpio_dwnld;
	static bool request = 0;
	const char *vdd_dwnld = NULL;
	static struct regulator *download_vdd = NULL;
	struct device_node *np;
	static unsigned int power_count = 0;

	np = of_find_node_by_name(NULL, "sprd-marlin");
	if (!np) {
		printk(KERN_ERR"sprd-marlin not found");
		return;
	}

	gpio_dwnld = of_get_gpio(np, 5);
	if (gpio_is_valid(gpio_dwnld)) {  /*gpio 1v6 power supply*/
		if(0 == request) {
			ret = gpio_request (gpio_dwnld, "download");
			if (ret){
				printk ("gpio_dwnld request err: %d\n", gpio_dwnld);
			}
		}
		else{
			request = 1;
		}

		if (enable) {
			gpio_direction_output(gpio_dwnld,1);

		}
		else {
			gpio_direction_output(gpio_dwnld,0);
		}
	} else { /*ldo 1v6 power supply*/

		ret = of_property_read_string(np, "vdd-download", &vdd_dwnld);
		if (ret) {
			printk(KERN_ERR"get vdd-download failed");
			return;
		}

		if (download_vdd == NULL) {
			download_vdd = regulator_get(NULL, vdd_dwnld);

			if (IS_ERR(download_vdd)) {
				printk(KERN_ERR"Get regulator of vdd-download  error!\n");
				return;
			}
		}
		#ifdef CONFIG_MACH_SHARKLS_J1MINI
		gpio_request(131,"vdd_pa");
		gpio_direction_output(131,1);
		gpio_set_value(131, 1);
		#endif

		mutex_lock(&power_ctl_lock);
		if(enable){
			if(0 == power_count){
				regulator_set_voltage(download_vdd, 1600000, 1600000);
				ret = regulator_enable(download_vdd);
				printk("sprd_download_poweron on\n");
			}
			power_count++;
		}
		else {
		    power_count--;
		    if(0 == power_count)
		    {
				if(regulator_is_enabled(download_vdd))
				ret = regulator_disable(download_vdd);
				printk("sprd_download_poweron off\n");
		    }
		}
		mutex_unlock(&power_ctl_lock);
	}
}

static void sprd_download_rst(void)
{
	int ret;
	u32 gpio_rst;
	static bool request = 0;
	struct device_node *np;

	np = of_find_node_by_name(NULL, "sprd-marlin");
	if (!np) {
		printk(KERN_ERR"sprd-marlin not found");
		return;
	}
	gpio_rst = of_get_gpio(np, 4);
	if(0 == request)
	{
		ret = gpio_request (gpio_rst, "download");
		if (ret){
			printk (KERN_ERR"gpio_rst request err: %d\n", gpio_rst);
		}
		else{
			request = 1;
		}
	}
	gpio_direction_output(gpio_rst,1);
	msleep(1);
	gpio_direction_output(gpio_rst,0);
	msleep(1);
	gpio_direction_output(gpio_rst,1);
	msleep(1);
}

static void sprd_gnss_chip_en(bool enable)
{
	if(enable) {
		gpio_direction_output(gnss_dev.chip_en,0);
		msleep(1);
		gpio_direction_output(gnss_dev.chip_en,1);
		msleep(1);
	}else {
		gpio_direction_output(gnss_dev.chip_en,0);
	}
}

static void gnss_lna_enable(bool on)
{
	int ret;

	if(gnss_dev.vdd_lna != NULL){
		if (on){
			regulator_set_voltage(gnss_dev.vdd_lna, 2800000, 2800000);
			ret = regulator_enable(gnss_dev.vdd_lna);
		}else if(regulator_is_enabled(gnss_dev.vdd_lna)) {
			ret = regulator_disable(gnss_dev.vdd_lna);
		}
	}
}

static long sprd_power_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
	printk("DOWNLOAD IOCTL: 0x%x.\n", cmd);

	switch (cmd) {
	case DOWNLOAD_POWER_ON:
		sprd_download_poweron(1);
		download_clk_init(1);
		break;
	case DOWNLOAD_POWER_OFF:
		sprd_download_poweron(0);
		break;
	case DOWNLOAD_POWER_RST:
		sprd_download_rst();
		break;
	case GNSS_CHIP_EN:
		sprd_gnss_chip_en(1);
		gnss_lna_enable(1);
		break;
	case GNSS_CHIP_DIS:
		sprd_gnss_chip_en(0);
		break;
	case GNSS_LNA_EN:
		gnss_lna_enable(1);
		break;
	case GNSS_LNA_DIS:
		gnss_lna_enable(0);
		break;
	}
	return 0;
}

static void sprd_download_dts_init(void)
{
	int ret;
	struct device_node *np;
	const char *vdd_lna = NULL;

	np = of_find_node_by_name(NULL, "sprd-ge2");
	if (!np) {
		printk(KERN_ERR"sprd-gnss not found");
		return;
	}

	ret = of_property_read_string(np, "vdd-lna", &vdd_lna);
	if (ret){
		printk(KERN_ERR"get vdd-lna failed");
		return;
	}

	gnss_dev.vdd_lna = regulator_get(NULL, vdd_lna);
	if (IS_ERR(gnss_dev.vdd_lna)){
		printk(KERN_ERR"Get regulator of vdd-lna  error!\n");
		return;
	}

	gnss_dev.chip_en = of_get_gpio(np, 0);
	ret = gpio_request (gnss_dev.chip_en, "ge2_chip_en");
	if (ret){
		printk (KERN_ERR"gnss_dev.chip_en request err: %d\n", gnss_dev.chip_en);
	}
}


#ifdef CONFIG_HAS_EARLYSUSPEND
static void gnss_early_suspend(struct early_suspend *handler)
{
	printk("%s\n",__func__);
	gnss_flag_sleep = true;
	gnss_flag_resume = false;
}

static void gnss_late_resume(struct early_suspend *handler)
{
	printk("%s\n",__func__);
	gnss_flag_resume = true;
	gnss_flag_sleep = false;
}

static unsigned int gnss_poll(struct file *filp, poll_table *wait)
{
	unsigned int mask = 0;

	poll_wait(filp, &gnss_sleep_wait, wait);
	if(gnss_flag_sleep)
	{
		printk("%s gnss_flag_sleep:%d\n",__func__,gnss_flag_sleep);
		gnss_flag_sleep = false;
		memcpy(gnss_status,"gnss_sleep ",11);
		mask |= POLLIN | POLLRDNORM;
	}

	if(gnss_flag_resume)
	{
		printk("%s gnss_flag_resume:%d\n",__func__,gnss_flag_resume);
		gnss_flag_resume = false;
		memcpy(gnss_status,"gnss_resume",11);
		mask |= POLLIN | POLLRDNORM;
	}

	return mask;
}

static int sprd_gnss_read(struct file *filp,char __user *buf,size_t count,loff_t *pos)
{
	if(count < 11)
		return -EINVAL;

	if(count > 11)
		count = 11;

	if(copy_to_user(buf,gnss_status,count)){
			return -EFAULT;
	}

	return count;
}
#endif

static struct file_operations sprd_power_ctl__fops = {
	.owner = THIS_MODULE,
	.unlocked_ioctl	= sprd_power_ctl_ioctl,
	#ifdef CONFIG_COMPAT
	.compat_ioctl = sprd_power_ctl_ioctl,
	#endif
	.open  = sprd_power_ctl_open,
	.release = sprd_power_ctl_release,
	#ifdef CONFIG_HAS_EARLYSUSPEND
	.read  = sprd_gnss_read,
	.poll  = gnss_poll,
	#endif
};

static struct miscdevice sprd_power_ctl_device = {
	.minor = MISC_DYNAMIC_MINOR,
	.name = POWER_CTL_NAME,
	.fops = &sprd_power_ctl__fops,
};

static int __init sprd_power_ctl_init(void)
{
	int err=0;
	printk("sprd_power_ctl_init\n");
	err = misc_register(&sprd_power_ctl_device);

	if(err){
		printk(KERN_INFO "download power control dev add failed!!!\n");
	}

	sprd_download_dts_init();

	#ifdef CONFIG_HAS_EARLYSUSPEND
	gnss_suspend.level = EARLY_SUSPEND_LEVEL_STOP_DRAWING + 30;
	gnss_suspend.suspend = gnss_early_suspend;
	gnss_suspend.resume	= gnss_late_resume;
	register_early_suspend(&gnss_suspend);

	init_waitqueue_head(&gnss_sleep_wait);
	#endif
	return err;
}

static void __exit sprd_power_ctl_cleanup(void)
{
	misc_deregister(&sprd_power_ctl_device);
}
module_init(sprd_power_ctl_init);
module_exit(sprd_power_ctl_cleanup);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("sprd download img driver");