summaryrefslogtreecommitdiffstats
path: root/target/linux/ubicom32/files/drivers/video/backlight/ubicom32bl.c
blob: 99538c341c910e536c108831af5d2569683d037a (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
/*
 * drivers/video/backlight/ubicom32bl.c
 *	Backlight driver for the Ubicom32 platform
 *
 * (C) Copyright 2009, Ubicom, Inc.
 *
 * This file is part of the Ubicom32 Linux Kernel Port.
 *
 * The Ubicom32 Linux Kernel Port is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, either version 2 of the
 * License, or (at your option) any later version.
 *
 * The Ubicom32 Linux Kernel Port is distributed in the hope that it
 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 * the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Ubicom32 Linux Kernel Port.  If not,
 * see <http://www.gnu.org/licenses/>.
 *
 * Ubicom32 implementation derived from (with many thanks):
 *   arch/m68knommu
 *   arch/blackfin
 *   arch/parisc
 */
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/backlight.h>
#include <linux/fb.h>

#include <asm/ubicom32bl.h>
#include <asm/ip5000.h>

#define DRIVER_NAME			"ubicom32bl"
#define UBICOM32BL_MAX_BRIGHTNESS	255

struct ubicom32bl_data {
	/*
	 * Pointer to the platform data structure.  Keep this around since we need values
	 * from it to set the backlight intensity.
	 */
	const struct ubicom32bl_platform_data	*pdata;

	/*
	 * Backlight device, we have to save this for use when we remove ourselves.
	 */
	struct backlight_device			*bldev;

	/*
	 * Current intensity, used for get_intensity.
	 */
	int					cur_intensity;

	/*
	 * Init function for PWM
	 */
	int (*init_fn)(struct ubicom32bl_data *);

	/*
	 * Set intensity function depending on the backlight type
	 */
	int (*set_intensity_fn)(struct ubicom32bl_data *, int);
};

/*
 * ubicom32bl_set_intensity_gpio
 */
static int ubicom32bl_set_intensity_gpio(struct ubicom32bl_data *ud, int intensity)
{
	ud->cur_intensity = intensity ? 255 : 0;

	if (intensity) {
		// set gpio
		return 0;
	}

	// clear gpio
	return 0;
}

/*
 * ubicom32bl_set_intensity_hw
 */
static int ubicom32bl_set_intensity_hw(struct ubicom32bl_data *ud, int intensity)
{
	u16_t period = ud->pdata->pwm_period;
	u16_t duty;

	/*
	 * Calculate the new duty cycle
	 */
	duty = (period * intensity) / (UBICOM32BL_MAX_BRIGHTNESS + 1);

	/*
	 * Set the new duty cycle
	 */
	switch (ud->pdata->pwm_channel) {
	case 0:
		/*
		 * Channel 0 is in the lower half of PORT C ctl0 and ctl1
		 */
		UBICOM32_IO_PORT(RC)->ctl1 = (ud->pdata->pwm_period << 16) | duty;
		break;

	case 1:
		/*
		 * Channel 1 is in the upper half of PORT C ctl0 and ctl2
		 */
		UBICOM32_IO_PORT(RC)->ctl2 = (ud->pdata->pwm_period << 16) | duty;
		break;

	case 2:
		/*
		 * Channel 2 is in PORT H ctl0 and ctl1
		 */
		UBICOM32_IO_PORT(RH)->ctl1 = (ud->pdata->pwm_period << 16) | duty;
		break;
	}

	ud->cur_intensity = intensity;

	return 0;
}

/*
 * ubicom32bl_set_intensity
 */
static int ubicom32bl_set_intensity(struct backlight_device *bd)
{
	struct ubicom32bl_data *ud = (struct ubicom32bl_data *)bl_get_data(bd);
	int intensity = bd->props.brightness;

	/*
	 * If we're blanked the the intensity doesn't matter.
	 */
	if ((bd->props.power != FB_BLANK_UNBLANK) || (bd->props.fb_blank != FB_BLANK_UNBLANK)) {
		intensity = 0;
	}

	/*
	 * Check for inverted backlight.
	 */
	if (ud->pdata->invert) {
		intensity = UBICOM32BL_MAX_BRIGHTNESS - intensity;
	}

	if (ud->set_intensity_fn) {
		return ud->set_intensity_fn(ud, intensity);
	}

	return -ENXIO;
}

/*
 * ubicom32bl_get_intensity
 *	Return the current intensity of the backlight.
 */
static int ubicom32bl_get_intensity(struct backlight_device *bd)
{
	struct ubicom32bl_data *ud = (struct ubicom32bl_data *)bl_get_data(bd);

	return ud->cur_intensity;
}

/*
 * ubicom32bl_init_hw_pwm
 *	Set the appropriate PWM registers
 */
static int ubicom32bl_init_hw_pwm(struct ubicom32bl_data *ud)
{
	/*
	 * bit 13: enable
	 */
	u16_t pwm_cfg = (1 << 13) | (ud->pdata->pwm_prescale << 8) ;

	switch (ud->pdata->pwm_channel) {
	case 0:
		/*
		 * Channel 0 is in the lower half of PORT C ctl0 and ctl1 (PA5)
		 */
		UBICOM32_IO_PORT(RC)->ctl0 &= ~0xFFFF;
		UBICOM32_IO_PORT(RC)->ctl0 |= pwm_cfg;
		UBICOM32_IO_PORT(RC)->ctl1 = ud->pdata->pwm_period << 16;

		/*
		 * If the port function is not set, set it to GPIO/PWM
		 */
		if (!UBICOM32_IO_PORT(RA)->function) {
			UBICOM32_IO_PORT(RA)->function = 3;
		}
		break;

	case 1:
		/*
		 * Channel 1 is in the upper half of PORT C ctl0 and ctl2 (PE4)
		 */
		UBICOM32_IO_PORT(RC)->ctl0 &= ~0xFFFF0000;
		UBICOM32_IO_PORT(RC)->ctl0 |= (pwm_cfg << 16);
		UBICOM32_IO_PORT(RC)->ctl2 = ud->pdata->pwm_period << 16;

		/*
		 * If the port function is not set, set it to GPIO/ExtIOInt
		 */
		if (!UBICOM32_IO_PORT(RE)->function) {
			UBICOM32_IO_PORT(RE)->function = 3;
		}
		break;

	case 2:
		/*
		 * Channel 2 is in PORT H ctl0 and ctl1 (PD0)
		 */
		UBICOM32_IO_PORT(RH)->ctl0 &= ~0xFFFF0000;
		UBICOM32_IO_PORT(RH)->ctl0 = pwm_cfg;
		UBICOM32_IO_PORT(RH)->ctl1 = ud->pdata->pwm_period << 16;

		/*
		 * If the port function is not set, set it to GPIO
		 */
		if (!UBICOM32_IO_PORT(RD)->function) {
			UBICOM32_IO_PORT(RD)->function = 3;
		}
		break;
	}

	return 0;
}

/*
 * ubicom32bl_init_gpio
 *	Allocate the appropriate GPIO
 */
static int ubicom32bl_init_gpio(struct ubicom32bl_data *ud)
{
	return 0;
}

static struct backlight_ops ubicom32bl_ops = {
	.get_brightness = ubicom32bl_get_intensity,
	.update_status  = ubicom32bl_set_intensity,
};

/*
 * ubicom32bl_probe
 */
static int ubicom32bl_probe(struct platform_device *pdev)
{
	const struct ubicom32bl_platform_data *pdata = pdev->dev.platform_data;
	struct ubicom32bl_data *ud;
	struct backlight_device *bldev;
	int retval;

	/*
	 * Check to see if we have any platform data, if we don't then the backlight is not
	 * configured on this device.
	 */
	if (!pdata) {
		return -ENODEV;
	}

	/*
	 * Allocate our private data
	 */
	ud = kzalloc(sizeof(struct ubicom32bl_data), GFP_KERNEL);
	if (!ud) {
		return -ENOMEM;
	}

	ud->pdata = pdata;

	/*
	 * Check to see that the platform data is valid for this driver
	 */
	switch (pdata->type) {
	case UBICOM32BL_TYPE_PWM:
		{
			/*
			 * Make sure we have a PWM peripheral
			 */
			u32_t chipid;
			asm volatile (
				"move.4         %0, CHIP_ID     \n\t"
				: "=r" (chipid)
			);
			if (chipid != 0x00030001) {
				retval = -ENODEV;
				goto fail;
			}

			if (pdata->pwm_channel > 3) {
				retval = -ENODEV;
				goto fail;
			}
			if (pdata->pwm_prescale > 16) {
				retval = -EINVAL;
				goto fail;
			}

			ud->init_fn = ubicom32bl_init_hw_pwm;
			ud->set_intensity_fn = ubicom32bl_set_intensity_hw;
			break;
		}

	case UBICOM32BL_TYPE_PWM_HRT:
		// For now, PWM HRT devices are treated as binary lights.

	case UBICOM32BL_TYPE_BINARY:
		ud->init_fn = ubicom32bl_init_gpio;
		ud->set_intensity_fn = ubicom32bl_set_intensity_gpio;
		break;
	}

	/*
	 * Register our backlight device
	 */
	bldev = backlight_device_register(DRIVER_NAME, &pdev->dev, ud, &ubicom32bl_ops);
	if (IS_ERR(bldev)) {
		retval = PTR_ERR(bldev);
		goto fail;
	}

	ud->bldev = bldev;
	ud->cur_intensity = pdata->default_intensity;
	platform_set_drvdata(pdev, ud);

	/*
	 * Start up the backlight at the prescribed default intensity
	 */
	bldev->props.power = FB_BLANK_UNBLANK;
	bldev->props.max_brightness = UBICOM32BL_MAX_BRIGHTNESS;
	bldev->props.brightness = pdata->default_intensity;

	if (ud->init_fn) {
		if (ud->init_fn(ud) != 0) {
			retval = -ENODEV;
			backlight_device_unregister(ud->bldev);
			goto fail;
		}
	}
	ubicom32bl_set_intensity(bldev);

	printk(KERN_INFO DRIVER_NAME ": Backlight driver started\n");

	return 0;

fail:
	platform_set_drvdata(pdev, NULL);
	kfree(ud);
	return retval;
}

/*
 * ubicom32bl_remove
 */
static int __exit ubicom32bl_remove(struct platform_device *pdev)
{
	struct ubicom32bl_data *ud = platform_get_drvdata(pdev);

	backlight_device_unregister(ud->bldev);
	platform_set_drvdata(pdev, NULL);
	kfree(ud);

	return 0;
}

static struct platform_driver ubicom32bl_driver = {
	.driver = {
		.name = DRIVER_NAME,
		.owner = THIS_MODULE,
	},

	.remove = __exit_p(ubicom32bl_remove),
};

/*
 * ubicom32bl_init
 */
static int __init ubicom32bl_init(void)
{
	return platform_driver_probe(&ubicom32bl_driver, ubicom32bl_probe);
}
module_init(ubicom32bl_init);

/*
 * ubicom32bl_exit
 */
static void __exit ubicom32bl_exit(void)
{
	platform_driver_unregister(&ubicom32bl_driver);
}
module_exit(ubicom32bl_exit);

MODULE_AUTHOR("Patrick Tjin <@ubicom.com>");
MODULE_DESCRIPTION("Ubicom32 backlight driver");
MODULE_LICENSE("GPL");