summaryrefslogtreecommitdiff
path: root/arch/arm/plat-mxc/dptc.c
blob: a26fd9b8d516e850f17eadb14f4cea407e55f59a (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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
/*
 * Copyright 2005-2010 Freescale Semiconductor, Inc. All Rights Reserved.
 */

/*
 * The code contained herein is licensed under the GNU General Public
 * License. You may obtain a copy of the GNU General Public License
 * Version 2 or later at the following locations:
 *
 * http://www.opensource.org/licenses/gpl-license.html
 * http://www.gnu.org/copyleft/gpl.html
 */

/*!
 * @file dptc.c
 *
 * @brief Driver for the Freescale Semiconductor MXC DPTC module.
 *
 * The DPTC driver is designed to control the MXC DPTC hardware.
 * hardware. Upon initialization, the DPTC driver initializes the DPTC hardware
 * sets up driver nodes attaches to the DPTC interrupt and initializes internal
 * data structures. When the DPTC interrupt occurs the driver checks the cause
 * of the interrupt (lower frequency, increase frequency or emergency) and changes
 * the CPU voltage according to translation table that is loaded into the driver.
 * The driver read method is used to read the log buffer.
 * Driver ioctls are used to change driver parameters and enable/disable the
 * DVFS operation.
 *
 * @ingroup PM
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/i2c.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/jiffies.h>
#include <linux/device.h>
#include <linux/delay.h>
#include <linux/clk.h>
#include <linux/workqueue.h>
#include <linux/platform_device.h>
#include <linux/regulator/consumer.h>

#include <mach/clock.h>
#include <mach/gpio.h>
#include <mach/hardware.h>
#include <mach/mxc_dptc.h>

/*
 * Convenience conversion.
 * Here atm, maybe there is somewhere better for this.
 */
#define mV_to_uV(mV) (mV * 1000)
#define uV_to_mV(uV) (uV / 1000)
#define V_to_uV(V) (mV_to_uV(V * 1000))
#define uV_to_V(uV) (uV_to_mV(uV) / 1000)

enum {
	DPTC_PTVAI_NOCHANGE = 0x0,
	DPTC_PTVAI_DECREASE,
	DPTC_PTVAI_INCREASE,
	DPTC_PTVAI_EMERG,
};

struct device *dev_data0;
struct device *dev_data1;
struct dptc_device *dptc_device_data;

/*!
 * In case the MXC device has multiple DPTC modules, this structure is used to
 * store information specific to each DPTC module.
 */
struct dptc_device {
	/* DPTC delayed work */
	struct delayed_work dptc_work;
	/* DPTC spinlock */
	spinlock_t lock;
	/* DPTC regulator */
	struct regulator *dptc_reg;
	/* DPTC clock */
	struct clk *dptc_clk;
	/* DPTC is active flag */
	int dptc_is_active;
	/* turbo mode active flag */
	int turbo_mode_active;
	/* DPTC current working point */
	int curr_wp;
	/* DPTC vai bits */
	u32 ptvai;
	/* The base address of the DPTC */
	void __iomem *membase;
	/* The interrupt number used by the DPTC device */
	int irq;
	/* DPTC platform data pointer */
	struct mxc_dptc_data *dptc_platform_data;
};

static void update_dptc_wp(struct dptc_device *drv_data, u32 wp)
{
	struct mxc_dptc_data *dptc_data = drv_data->dptc_platform_data;
	int voltage_uV;
	int ret = 0;

	voltage_uV = dptc_data->dptc_wp_allfreq[wp].voltage * 1000;

	__raw_writel(dptc_data->dptc_wp_allfreq[wp].dcvr0,
		     drv_data->membase + dptc_data->dcvr0_reg_addr);
	__raw_writel(dptc_data->dptc_wp_allfreq[wp].dcvr1,
		     drv_data->membase + dptc_data->dcvr0_reg_addr + 0x4);
	__raw_writel(dptc_data->dptc_wp_allfreq[wp].dcvr2,
		     drv_data->membase + dptc_data->dcvr0_reg_addr + 0x8);
	__raw_writel(dptc_data->dptc_wp_allfreq[wp].dcvr3,
		     drv_data->membase + dptc_data->dcvr0_reg_addr + 0xC);

	/* Set the voltage */
	ret = regulator_set_voltage(drv_data->dptc_reg, voltage_uV, voltage_uV);
	if (ret < 0)
		printk(KERN_DEBUG "COULD NOT SET VOLTAGE!!!!!\n");

	pr_debug("dcvr0-3: 0x%x, 0x%x, 0x%x, 0x%x; vol: %d\n",
		 dptc_data->dptc_wp_allfreq[wp].dcvr0,
		 dptc_data->dptc_wp_allfreq[wp].dcvr1,
		 dptc_data->dptc_wp_allfreq[wp].dcvr2,
		 dptc_data->dptc_wp_allfreq[wp].dcvr3,
		 dptc_data->dptc_wp_allfreq[wp].voltage);
}

static irqreturn_t dptc_irq(int irq, void *dev_id)
{
	struct device *dev = dev_id;
	struct dptc_device *drv_data = dev->driver_data;
	struct mxc_dptc_data *dptc_data = dev->platform_data;
	u32 dptccr = __raw_readl(drv_data->membase
				 + dptc_data->dptccr_reg_addr);
	u32 gpc_cntr = __raw_readl(dptc_data->gpc_cntr_reg_addr);

	gpc_cntr = (gpc_cntr & dptc_data->dptccr);

	if (gpc_cntr) {
		drv_data->ptvai =
		    (dptccr & dptc_data->vai_mask) >> dptc_data->vai_offset;
		pr_debug("dptc_irq: vai = 0x%x (0x%x)!!!!!!!\n",
			 drv_data->ptvai, dptccr);

		/* disable DPTC and mask its interrupt */
		dptccr = (dptccr & ~(dptc_data->dptc_enable_bit)) |
		    (dptc_data->irq_mask);
		dptccr = (dptccr & ~(dptc_data->dptc_nvcr_bit));
		__raw_writel(dptccr, drv_data->membase
			     + dptc_data->dptccr_reg_addr);

		if (drv_data->turbo_mode_active == 1)
			schedule_delayed_work(&drv_data->dptc_work, 0);
		return IRQ_HANDLED;
	}

	return IRQ_NONE;
}

static void dptc_workqueue_handler(struct work_struct *work1)
{
	struct delayed_work *dptc_work_tmp =
	    container_of(work1, struct delayed_work, work);
	struct dptc_device *drv_data =
	    container_of(dptc_work_tmp, struct dptc_device, dptc_work);
	struct mxc_dptc_data *dptc_data = drv_data->dptc_platform_data;
	u32 dptccr = __raw_readl(drv_data->membase
				 + dptc_data->dptccr_reg_addr);

	switch (drv_data->ptvai) {
	case DPTC_PTVAI_DECREASE:
		drv_data->curr_wp++;
		break;
	case DPTC_PTVAI_INCREASE:
	case DPTC_PTVAI_EMERG:
		drv_data->curr_wp--;
		if (drv_data->curr_wp < 0) {
			/* already max voltage */
			drv_data->curr_wp = 0;
			printk(KERN_WARNING "dptc: already maximum voltage\n");
		}
		break;

		/* Unknown interrupt cause */
	default:
		BUG();
	}

	if (drv_data->curr_wp > dptc_data->dptc_wp_supported
	    || drv_data->curr_wp < 0) {
		panic("Can't support this working point: %d\n",
		      drv_data->curr_wp);
	}
	update_dptc_wp(drv_data, drv_data->curr_wp);

	/* Enable DPTC and unmask its interrupt */
	dptccr = (dptccr & ~(dptc_data->irq_mask)) |
	    dptc_data->dptc_nvcr_bit | dptc_data->dptc_enable_bit;
	__raw_writel(dptccr, drv_data->membase + dptc_data->dptccr_reg_addr);
}

/* Start DPTC unconditionally */
static int start_dptc(struct device *dev)
{
	struct mxc_dptc_data *dptc_data = dev->platform_data;
	struct dptc_device *drv_data = dev->driver_data;
	u32 dptccr;
	unsigned long flags;
	unsigned long clk_rate;
	int voltage_uV;

	/* Get the voltage */
	voltage_uV = regulator_get_voltage(drv_data->dptc_reg);
	drv_data->curr_wp =
	    (dptc_data->dptc_wp_allfreq[0].voltage - (voltage_uV / 1000)) / 25;

	update_dptc_wp(drv_data, drv_data->curr_wp);

	/* Set the voltage */
	spin_lock_irqsave(&drv_data->lock, flags);

	clk_rate = clk_get_rate(drv_data->dptc_clk);

	if (clk_rate < dptc_data->clk_max_val)
		goto err;

	if (dptc_data->gpc_irq_bit != 0x0) {
		/* Enable ARM domain frequency and/or voltage update needed
		   and enable ARM IRQ */
		__raw_writel(dptc_data->gpc_irq_bit | dptc_data->gpc_adu,
			     dptc_data->gpc_cntr_reg_addr);
	}

	dptccr = __raw_readl(drv_data->membase + dptc_data->dptccr_reg_addr);

	/* Enable DPTC and unmask its interrupt */
	dptccr = ((dptccr & ~(dptc_data->irq_mask)) | dptc_data->enable_config);

	__raw_writel(dptccr, drv_data->membase + dptc_data->dptccr_reg_addr);

	spin_unlock_irqrestore(&drv_data->lock, flags);

	drv_data->dptc_is_active = 1;
	drv_data->turbo_mode_active = 1;

	pr_info("DPTC has been started \n");

	return 0;

err:
	spin_unlock_irqrestore(&drv_data->lock, flags);
	pr_info("DPTC is not enabled\n");
	return -1;
}

/* Stop DPTC unconditionally */
static void stop_dptc(struct device *dev)
{
	struct mxc_dptc_data *dptc_data = dev->platform_data;
	struct dptc_device *drv_data = dev->driver_data;
	u32 dptccr;

	dptccr = __raw_readl(drv_data->membase + dptc_data->dptccr_reg_addr);

	/* disable DPTC and mask its interrupt */
	dptccr = ((dptccr & ~(dptc_data->dptc_enable_bit)) |
		  dptc_data->irq_mask) & (~dptc_data->dptc_nvcr_bit);

	__raw_writel(dptccr, drv_data->membase + dptc_data->dptccr_reg_addr);

	/* Restore Turbo Mode voltage to highest wp */
	update_dptc_wp(drv_data, 0);
	drv_data->curr_wp = 0;

	regulator_put(drv_data->dptc_reg);

	pr_info("DPTC has been stopped\n");
}

/*
  This function does not change the working point. It can be
 called from an interrupt context.
*/
void dptc_suspend(int id)
{
	struct mxc_dptc_data *dptc_data;
	struct dptc_device *drv_data;
	u32 dptccr;

	switch (id) {
	case DPTC_GP_ID:
		dptc_data = dev_data0->platform_data;
		drv_data = dev_data0->driver_data;
		break;
	case DPTC_LP_ID:
		if (dev_data1 == NULL)
			return;

		dptc_data = dev_data1->platform_data;
		drv_data = dev_data1->driver_data;
		break;
		/* Unknown DPTC ID */
	default:
		return;
	}

	if (!drv_data->dptc_is_active)
		return;

	dptccr = __raw_readl(drv_data->membase + dptc_data->dptccr_reg_addr);

	/* Disable DPTC and mask its interrupt */
	dptccr = (dptccr & ~(dptc_data->dptc_enable_bit)) | dptc_data->irq_mask;

	__raw_writel(dptccr, drv_data->membase + dptc_data->dptccr_reg_addr);
}
EXPORT_SYMBOL(dptc_suspend);

/*
  This function does not change the working point. It can be
 called from an interrupt context.
*/
void dptc_resume(int id)
{
	struct mxc_dptc_data *dptc_data;
	struct dptc_device *drv_data;
	u32 dptccr;

	switch (id) {
	case DPTC_GP_ID:
		dptc_data = dev_data0->platform_data;
		drv_data = dev_data0->driver_data;
		break;
	case DPTC_LP_ID:
		if (dev_data1 == NULL)
			return;

		dptc_data = dev_data1->platform_data;
		drv_data = dev_data1->driver_data;
		break;
		/* Unknown DPTC ID */
	default:
		return;
	}

	if (!drv_data->dptc_is_active)
		return;

	__raw_writel(dptc_data->dptc_wp_allfreq[0].dcvr0,
		     drv_data->membase + dptc_data->dcvr0_reg_addr);
	__raw_writel(dptc_data->dptc_wp_allfreq[0].dcvr1,
		     drv_data->membase + dptc_data->dcvr0_reg_addr + 0x4);
	__raw_writel(dptc_data->dptc_wp_allfreq[0].dcvr2,
		     drv_data->membase + dptc_data->dcvr0_reg_addr + 0x8);
	__raw_writel(dptc_data->dptc_wp_allfreq[0].dcvr3,
		     drv_data->membase + dptc_data->dcvr0_reg_addr + 0xC);

	dptccr = __raw_readl(drv_data->membase + dptc_data->dptccr_reg_addr);

	/* Enable DPTC and unmask its interrupt */
	dptccr = (dptccr & ~(dptc_data->irq_mask)) | dptc_data->dptc_enable_bit;

	__raw_writel(dptccr, drv_data->membase + dptc_data->dptccr_reg_addr);
}
EXPORT_SYMBOL(dptc_resume);

/*!
 * This function is called to put the DPTC in a low power state.
 *
 */
void dptc_disable(struct device *dev)
{
	struct dptc_device *drv_data = dev->driver_data;

	if (!(drv_data->dptc_is_active))
		return;

	stop_dptc(dev);
	drv_data->dptc_is_active = 0;
	drv_data->turbo_mode_active = 0;
}

/*!
 * This function is called to resume the DPTC from a low power state.
 *
 */
int dptc_enable(struct device *dev)
{
	struct dptc_device *drv_data = dev->driver_data;

	if (drv_data->dptc_is_active)
		return 0;

	return start_dptc(dev);
}

static ssize_t dptc_show(struct device *dev, struct device_attribute *attr,
			 char *buf)
{
	struct dptc_device *drv_data = dev->driver_data;

	if (drv_data->dptc_is_active)
		return sprintf(buf, "DPTC is enabled\n");
	else
		return sprintf(buf, "DPTC is disabled\n");
}

static ssize_t dptc_store(struct device *dev, struct device_attribute *attr,
			  const char *buf, size_t size)
{
	if (strstr(buf, "0") != NULL) {
		dptc_disable(dev);
	} else if (strstr(buf, "1") != NULL) {
		dptc_enable(dev);
	}

	return size;
}

static DEVICE_ATTR(enable, 0644, dptc_show, dptc_store);

/*!
 * This is the probe routine for the DPTC driver.
 *
 * @param   pdev   The platform device structure
 *
 * @return         The function returns 0 on success
 *
 */
static int __devinit mxc_dptc_probe(struct platform_device *pdev)
{
	int ret = 0;
	struct resource *res;
	u32 dptccr = 0;
	struct clk *ckih_clk;
	struct mxc_dptc_data *dptc_data = pdev->dev.platform_data;

	if (dptc_data == NULL) {
		printk(KERN_ERR "DPTC: Pointer to DPTC data is NULL\
				not started\n");
		return -1;
	}

	dptc_device_data = kzalloc(sizeof(struct dptc_device), GFP_KERNEL);
	if (!dptc_device_data)
		return -ENOMEM;

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (res == NULL) {
		ret = -ENODEV;
		goto err1;
	}

	dptc_device_data->membase = ioremap(res->start,
					    res->end - res->start + 1);

	/*
	 * Request the DPTC interrupt
	 */
	dptc_device_data->irq = platform_get_irq(pdev, 0);
	if (dptc_device_data->irq < 0) {
		ret = dptc_device_data->irq;
		goto err2;
	}

	ret =
	    request_irq(dptc_device_data->irq, dptc_irq, IRQF_SHARED,
			pdev->name, &pdev->dev);
	if (ret) {
		printk(KERN_ERR "DPTC: Unable to attach to DPTC interrupt\n");
		goto err2;
	}

	dptc_device_data->curr_wp = 0;
	dptc_device_data->dptc_is_active = 0;
	dptc_device_data->turbo_mode_active = 0;
	dptc_device_data->ptvai = 0;

	dptccr = __raw_readl(dptc_device_data->membase
			     + dptc_data->dptccr_reg_addr);

	printk(KERN_INFO "DPTC mxc_dptc_probe()\n");

	spin_lock_init(&dptc_device_data->lock);

	if (dptc_data->dptc_wp_allfreq == NULL) {
		ckih_clk = clk_get(NULL, "ckih");
		if (cpu_is_mx31() &
		    (mxc_cpu_is_rev(CHIP_REV_2_0) < 0) &
		    (clk_get_rate(ckih_clk) == 27000000))
			printk(KERN_ERR "DPTC: DPTC not supported on TO1.x \
					& ckih = 27M\n");
		else
			printk(KERN_ERR "DPTC: Pointer to DPTC table is NULL\
					not started\n");
		goto err3;
	}

	dptc_device_data->dptc_reg = regulator_get(NULL, dptc_data->reg_id);
	if (IS_ERR(dptc_device_data->dptc_reg)) {
		clk_put(dptc_device_data->dptc_clk);
		printk(KERN_ERR "%s: failed to get regulator\n", __func__);
		goto err3;
	}

	INIT_DELAYED_WORK(&dptc_device_data->dptc_work, dptc_workqueue_handler);

	/* Enable Reference Circuits */
	dptccr = (dptccr & ~(dptc_data->dcr_mask)) | dptc_data->init_config;
	__raw_writel(dptccr, dptc_device_data->membase
			     + dptc_data->dptccr_reg_addr);

	ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_enable.attr);
	if (ret) {
		printk(KERN_ERR
		       "DPTC: Unable to register sysdev entry for dptc");
		goto err3;
	}

	if (ret != 0) {
		printk(KERN_ERR "DPTC: Unable to start");
		goto err3;
	}

	dptc_device_data->dptc_clk = clk_get(NULL, dptc_data->clk_id);

	if (pdev->id == 0)
		dev_data0 = &pdev->dev;
	else
		dev_data1 = &pdev->dev;

	dptc_device_data->dptc_platform_data = pdev->dev.platform_data;

	/* Set driver data */
	platform_set_drvdata(pdev, dptc_device_data);

	return 0;

err3:
	free_irq(dptc_device_data->irq, &pdev->dev);
err2:
	iounmap(dptc_device_data->membase);
err1:
	dev_err(&pdev->dev, "Failed to probe DPTC\n");
	kfree(dptc_device_data);
	return ret;
}

/*!
 * This function is called to put DPTC in a low power state.
 *
 * @param   pdev  the device structure
 * @param   state the power state the device is entering
 *
 * @return  The function always returns 0.
 */
static int mxc_dptc_suspend(struct platform_device *pdev, pm_message_t state)
{
	struct dptc_device *drv_data = pdev->dev.driver_data;

	if (drv_data->dptc_is_active)
		stop_dptc(&pdev->dev);

	return 0;
}

/*!
 * This function is called to resume the MU from a low power state.
 *
 * @param   dev   the device structure
 * @param   level the stage in device suspension process that we want the
 *                device to be put in
 *
 * @return  The function always returns 0.
 */
static int mxc_dptc_resume(struct platform_device *pdev)
{
	struct dptc_device *drv_data = pdev->dev.driver_data;

	if (drv_data->dptc_is_active)
		return start_dptc(&pdev->dev);

	return 0;
}

static struct platform_driver mxc_dptc_driver = {
	.driver = {
		   .name = "mxc_dptc",
		   .owner = THIS_MODULE,
		   },
	.probe = mxc_dptc_probe,
	.suspend = mxc_dptc_suspend,
	.resume = mxc_dptc_resume,
};

/*!
 * This function is called to resume the MU from a low power state.
 *
 * @param   dev   the device structure used to give information on which MU
 *                device (0 through 3 channels) to suspend
 * @param   level the stage in device suspension process that we want the
 *                device to be put in
 *
 * @return  The function always returns 0.
 */

static int __init dptc_init(void)
{
	if (platform_driver_register(&mxc_dptc_driver) != 0) {
		printk(KERN_ERR "mxc_dptc_driver register failed\n");
		return -ENODEV;
	}

	printk(KERN_INFO "DPTC driver module loaded\n");

	return 0;
}

static void __exit dptc_cleanup(void)
{
	free_irq(dptc_device_data->irq, NULL);
	iounmap(dptc_device_data->membase);
	kfree(dptc_device_data);

	/* Unregister the device structure */
	platform_driver_unregister(&mxc_dptc_driver);

	printk("DPTC driver module unloaded\n");
}

module_init(dptc_init);
module_exit(dptc_cleanup);

MODULE_AUTHOR("Freescale Semiconductor, Inc.");
MODULE_DESCRIPTION("DPTC driver");
MODULE_LICENSE("GPL");