Merge tag 'pm+acpi-for-3.7-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git...
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / staging / olpc_dcon / olpc_dcon.c
CommitLineData
eecb3e4e
AS
1/*
2 * Mainly by David Woodhouse, somewhat modified by Jordan Crouse
3 *
4 * Copyright © 2006-2007 Red Hat, Inc.
5 * Copyright © 2006-2007 Advanced Micro Devices, Inc.
6 * Copyright © 2009 VIA Technology, Inc.
097cd83a 7 * Copyright (c) 2010-2011 Andres Salomon <dilinger@queued.net>
eecb3e4e
AS
8 *
9 * This program is free software. You can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
12 */
13
ac9bbd08 14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
eecb3e4e
AS
15
16#include <linux/kernel.h>
17#include <linux/fb.h>
18#include <linux/console.h>
19#include <linux/i2c.h>
20#include <linux/platform_device.h>
eecb3e4e 21#include <linux/pci.h>
eecb3e4e
AS
22#include <linux/pci_ids.h>
23#include <linux/interrupt.h>
24#include <linux/delay.h>
99c97852 25#include <linux/module.h>
eecb3e4e
AS
26#include <linux/backlight.h>
27#include <linux/device.h>
e107e6eb 28#include <linux/uaccess.h>
eecb3e4e
AS
29#include <linux/ctype.h>
30#include <linux/reboot.h>
3bf9428f 31#include <linux/olpc-ec.h>
eecb3e4e
AS
32#include <asm/tsc.h>
33#include <asm/olpc.h>
34
35#include "olpc_dcon.h"
36
37/* Module definitions */
38
c542341d
VR
39static ushort resumeline = 898;
40module_param(resumeline, ushort, 0444);
eecb3e4e 41
eecb3e4e
AS
42/* Default off since it doesn't work on DCON ASIC in B-test OLPC board */
43static int useaa = 1;
44module_param(useaa, int, 0444);
45
eecb3e4e
AS
46static struct dcon_platform_data *pdata;
47
48/* I2C structures */
49
eecb3e4e
AS
50/* Platform devices */
51static struct platform_device *dcon_device;
52
eecb3e4e
AS
53static DECLARE_WAIT_QUEUE_HEAD(dcon_wait_queue);
54
55static unsigned short normal_i2c[] = { 0x0d, I2C_CLIENT_END };
56
8d2d3dd1
AS
57static s32 dcon_write(struct dcon_priv *dcon, u8 reg, u16 val)
58{
59 return i2c_smbus_write_word_data(dcon->client, reg, val);
60}
61
62static s32 dcon_read(struct dcon_priv *dcon, u8 reg)
63{
64 return i2c_smbus_read_word_data(dcon->client, reg);
65}
eecb3e4e 66
eecb3e4e
AS
67/* ===== API functions - these are called by a variety of users ==== */
68
8d2d3dd1 69static int dcon_hw_init(struct dcon_priv *dcon, int is_init)
eecb3e4e
AS
70{
71 uint16_t ver;
72 int rc = 0;
73
0b7a41eb 74 ver = dcon_read(dcon, DCON_REG_ID);
eecb3e4e 75 if ((ver >> 8) != 0xDC) {
ac9bbd08 76 pr_err("DCON ID not 0xDCxx: 0x%04x instead.\n", ver);
eecb3e4e
AS
77 rc = -ENXIO;
78 goto err;
79 }
80
81 if (is_init) {
ac9bbd08 82 pr_info("Discovered DCON version %x\n", ver & 0xFF);
bbe963f1 83 rc = pdata->init(dcon);
e107e6eb 84 if (rc != 0) {
ac9bbd08 85 pr_err("Unable to init.\n");
eecb3e4e
AS
86 goto err;
87 }
88 }
89
24e26170 90 if (ver < 0xdc02) {
1b995ac2
AS
91 dev_err(&dcon->client->dev,
92 "DCON v1 is unsupported, giving up..\n");
93 rc = -ENODEV;
94 goto err;
eecb3e4e
AS
95 }
96
1b995ac2 97 /* SDRAM setup/hold time */
0b7a41eb
AS
98 dcon_write(dcon, 0x3a, 0xc040);
99 dcon_write(dcon, 0x41, 0x0000);
100 dcon_write(dcon, 0x41, 0x0101);
101 dcon_write(dcon, 0x42, 0x0101);
1b995ac2 102
eecb3e4e
AS
103 /* Colour swizzle, AA, no passthrough, backlight */
104 if (is_init) {
bada46e5
AS
105 dcon->disp_mode = MODE_PASSTHRU | MODE_BL_ENABLE |
106 MODE_CSWIZZLE;
eecb3e4e 107 if (useaa)
bada46e5 108 dcon->disp_mode |= MODE_COL_AA;
eecb3e4e 109 }
0b7a41eb 110 dcon_write(dcon, DCON_REG_MODE, dcon->disp_mode);
eecb3e4e
AS
111
112
113 /* Set the scanline to interrupt on during resume */
0b7a41eb 114 dcon_write(dcon, DCON_REG_SCAN_INT, resumeline);
eecb3e4e
AS
115
116err:
117 return rc;
118}
119
120/*
121 * The smbus doesn't always come back due to what is believed to be
122 * hardware (power rail) bugs. For older models where this is known to
123 * occur, our solution is to attempt to wait for the bus to stabilize;
124 * if it doesn't happen, cut power to the dcon, repower it, and wait
125 * for the bus to stabilize. Rinse, repeat until we have a working
126 * smbus. For newer models, we simply BUG(); we want to know if this
127 * still happens despite the power fixes that have been made!
128 */
8d2d3dd1 129static int dcon_bus_stabilize(struct dcon_priv *dcon, int is_powered_down)
eecb3e4e
AS
130{
131 unsigned long timeout;
132 int x;
133
134power_up:
135 if (is_powered_down) {
136 x = 1;
c2562687 137 x = olpc_ec_cmd(0x26, (unsigned char *)&x, 1, NULL, 0);
e107e6eb 138 if (x) {
ac9bbd08 139 pr_warn("unable to force dcon to power up: %d!\n", x);
eecb3e4e
AS
140 return x;
141 }
142 msleep(10); /* we'll be conservative */
143 }
e107e6eb 144
eecb3e4e
AS
145 pdata->bus_stabilize_wiggle();
146
147 for (x = -1, timeout = 50; timeout && x < 0; timeout--) {
148 msleep(1);
8d2d3dd1 149 x = dcon_read(dcon, DCON_REG_ID);
eecb3e4e
AS
150 }
151 if (x < 0) {
ac9bbd08 152 pr_err("unable to stabilize dcon's smbus, reasserting power and praying.\n");
316604be 153 BUG_ON(olpc_board_at_least(olpc_board(0xc2)));
eecb3e4e 154 x = 0;
c2562687 155 olpc_ec_cmd(0x26, (unsigned char *)&x, 1, NULL, 0);
eecb3e4e
AS
156 msleep(100);
157 is_powered_down = 1;
158 goto power_up; /* argh, stupid hardware.. */
159 }
160
161 if (is_powered_down)
8d2d3dd1 162 return dcon_hw_init(dcon, 0);
eecb3e4e
AS
163 return 0;
164}
165
c59eef17 166static void dcon_set_backlight(struct dcon_priv *dcon, u8 level)
eecb3e4e 167{
c59eef17
AS
168 dcon->bl_val = level;
169 dcon_write(dcon, DCON_REG_BRIGHT, dcon->bl_val);
eecb3e4e
AS
170
171 /* Purposely turn off the backlight when we go to level 0 */
c59eef17 172 if (dcon->bl_val == 0) {
bada46e5
AS
173 dcon->disp_mode &= ~MODE_BL_ENABLE;
174 dcon_write(dcon, DCON_REG_MODE, dcon->disp_mode);
175 } else if (!(dcon->disp_mode & MODE_BL_ENABLE)) {
176 dcon->disp_mode |= MODE_BL_ENABLE;
177 dcon_write(dcon, DCON_REG_MODE, dcon->disp_mode);
eecb3e4e
AS
178 }
179}
180
eecb3e4e 181/* Set the output type to either color or mono */
bb410354 182static int dcon_set_mono_mode(struct dcon_priv *dcon, bool enable_mono)
eecb3e4e 183{
bb410354 184 if (dcon->mono == enable_mono)
eecb3e4e
AS
185 return 0;
186
bb410354 187 dcon->mono = enable_mono;
eecb3e4e 188
bb410354 189 if (enable_mono) {
bada46e5
AS
190 dcon->disp_mode &= ~(MODE_CSWIZZLE | MODE_COL_AA);
191 dcon->disp_mode |= MODE_MONO_LUMA;
e107e6eb 192 } else {
bada46e5
AS
193 dcon->disp_mode &= ~(MODE_MONO_LUMA);
194 dcon->disp_mode |= MODE_CSWIZZLE;
eecb3e4e 195 if (useaa)
bada46e5 196 dcon->disp_mode |= MODE_COL_AA;
eecb3e4e
AS
197 }
198
bada46e5 199 dcon_write(dcon, DCON_REG_MODE, dcon->disp_mode);
eecb3e4e
AS
200 return 0;
201}
202
203/* For now, this will be really stupid - we need to address how
204 * DCONLOAD works in a sleep and account for it accordingly
205 */
206
bada46e5 207static void dcon_sleep(struct dcon_priv *dcon, bool sleep)
eecb3e4e
AS
208{
209 int x;
210
211 /* Turn off the backlight and put the DCON to sleep */
212
bada46e5 213 if (dcon->asleep == sleep)
eecb3e4e
AS
214 return;
215
316604be 216 if (!olpc_board_at_least(olpc_board(0xc2)))
eecb3e4e
AS
217 return;
218
bada46e5 219 if (sleep) {
eecb3e4e 220 x = 0;
c2562687 221 x = olpc_ec_cmd(0x26, (unsigned char *)&x, 1, NULL, 0);
e107e6eb 222 if (x)
ac9bbd08 223 pr_warn("unable to force dcon to power down: %d!\n", x);
eecb3e4e 224 else
bada46e5 225 dcon->asleep = sleep;
e107e6eb 226 } else {
eecb3e4e 227 /* Only re-enable the backlight if the backlight value is set */
c59eef17 228 if (dcon->bl_val != 0)
bada46e5 229 dcon->disp_mode |= MODE_BL_ENABLE;
8d2d3dd1 230 x = dcon_bus_stabilize(dcon, 1);
e107e6eb 231 if (x)
ac9bbd08 232 pr_warn("unable to reinit dcon hardware: %d!\n", x);
eecb3e4e 233 else
bada46e5 234 dcon->asleep = sleep;
eecb3e4e
AS
235
236 /* Restore backlight */
c59eef17 237 dcon_set_backlight(dcon, dcon->bl_val);
eecb3e4e
AS
238 }
239
240 /* We should turn off some stuff in the framebuffer - but what? */
241}
242
243/* the DCON seems to get confused if we change DCONLOAD too
e107e6eb 244 * frequently -- i.e., approximately faster than frame time.
eecb3e4e
AS
245 * normally we don't change it this fast, so in general we won't
246 * delay here.
247 */
309ef2a2 248static void dcon_load_holdoff(struct dcon_priv *dcon)
eecb3e4e
AS
249{
250 struct timespec delta_t, now;
e107e6eb 251 while (1) {
eecb3e4e 252 getnstimeofday(&now);
309ef2a2 253 delta_t = timespec_sub(now, dcon->load_time);
eecb3e4e
AS
254 if (delta_t.tv_sec != 0 ||
255 delta_t.tv_nsec > NSEC_PER_MSEC * 20) {
256 break;
257 }
258 mdelay(4);
259 }
260}
eecb3e4e 261
45bfe972
AS
262static bool dcon_blank_fb(struct dcon_priv *dcon, bool blank)
263{
264 int err;
265
266 if (!lock_fb_info(dcon->fbinfo)) {
267 dev_err(&dcon->client->dev, "unable to lock framebuffer\n");
268 return false;
269 }
270 console_lock();
271 dcon->ignore_fb_events = true;
272 err = fb_blank(dcon->fbinfo,
273 blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK);
274 dcon->ignore_fb_events = false;
275 console_unlock();
276 unlock_fb_info(dcon->fbinfo);
277
278 if (err) {
279 dev_err(&dcon->client->dev, "couldn't %sblank framebuffer\n",
280 blank ? "" : "un");
281 return false;
282 }
283 return true;
284}
285
286/* Set the source of the display (CPU or DCON) */
eecb3e4e
AS
287static void dcon_source_switch(struct work_struct *work)
288{
8d2d3dd1
AS
289 struct dcon_priv *dcon = container_of(work, struct dcon_priv,
290 switch_source);
eecb3e4e 291 DECLARE_WAITQUEUE(wait, current);
bbe963f1 292 int source = dcon->pending_src;
eecb3e4e 293
bbe963f1 294 if (dcon->curr_src == source)
eecb3e4e
AS
295 return;
296
309ef2a2 297 dcon_load_holdoff(dcon);
eecb3e4e 298
309ef2a2 299 dcon->switched = false;
eecb3e4e
AS
300
301 switch (source) {
302 case DCON_SOURCE_CPU:
ac9bbd08 303 pr_info("dcon_source_switch to CPU\n");
eecb3e4e 304 /* Enable the scanline interrupt bit */
8d2d3dd1 305 if (dcon_write(dcon, DCON_REG_MODE,
bada46e5 306 dcon->disp_mode | MODE_SCAN_INT))
ac9bbd08 307 pr_err("couldn't enable scanline interrupt!\n");
eecb3e4e
AS
308 else {
309 /* Wait up to one second for the scanline interrupt */
e107e6eb 310 wait_event_timeout(dcon_wait_queue,
309ef2a2 311 dcon->switched == true, HZ);
eecb3e4e
AS
312 }
313
309ef2a2 314 if (!dcon->switched)
ac9bbd08 315 pr_err("Timeout entering CPU mode; expect a screen glitch.\n");
eecb3e4e
AS
316
317 /* Turn off the scanline interrupt */
bada46e5 318 if (dcon_write(dcon, DCON_REG_MODE, dcon->disp_mode))
ac9bbd08 319 pr_err("couldn't disable scanline interrupt!\n");
eecb3e4e
AS
320
321 /*
322 * Ideally we'd like to disable interrupts here so that the
323 * fb unblanking and DCON turn on happen at a known time value;
324 * however, we can't do that right now with fb_blank
325 * messing with semaphores.
326 *
327 * For now, we just hope..
328 */
45bfe972 329 if (!dcon_blank_fb(dcon, false)) {
ac9bbd08 330 pr_err("Failed to enter CPU mode\n");
bbe963f1 331 dcon->pending_src = DCON_SOURCE_DCON;
eecb3e4e
AS
332 return;
333 }
eecb3e4e
AS
334
335 /* And turn off the DCON */
336 pdata->set_dconload(1);
309ef2a2 337 getnstimeofday(&dcon->load_time);
eecb3e4e 338
ac9bbd08 339 pr_info("The CPU has control\n");
eecb3e4e
AS
340 break;
341 case DCON_SOURCE_DCON:
342 {
343 int t;
344 struct timespec delta_t;
345
ac9bbd08 346 pr_info("dcon_source_switch to DCON\n");
eecb3e4e
AS
347
348 add_wait_queue(&dcon_wait_queue, &wait);
349 set_current_state(TASK_UNINTERRUPTIBLE);
350
351 /* Clear DCONLOAD - this implies that the DCON is in control */
352 pdata->set_dconload(0);
309ef2a2 353 getnstimeofday(&dcon->load_time);
eecb3e4e
AS
354
355 t = schedule_timeout(HZ/2);
356 remove_wait_queue(&dcon_wait_queue, &wait);
357 set_current_state(TASK_RUNNING);
358
309ef2a2 359 if (!dcon->switched) {
ac9bbd08 360 pr_err("Timeout entering DCON mode; expect a screen glitch.\n");
eecb3e4e
AS
361 } else {
362 /* sometimes the DCON doesn't follow its own rules,
363 * and doesn't wait for two vsync pulses before
364 * ack'ing the frame load with an IRQ. the result
365 * is that the display shows the *previously*
366 * loaded frame. we can detect this by looking at
367 * the time between asserting DCONLOAD and the IRQ --
368 * if it's less than 20msec, then the DCON couldn't
369 * have seen two VSYNC pulses. in that case we
e107e6eb 370 * deassert and reassert, and hope for the best.
eecb3e4e
AS
371 * see http://dev.laptop.org/ticket/9664
372 */
309ef2a2
AS
373 delta_t = timespec_sub(dcon->irq_time, dcon->load_time);
374 if (dcon->switched && delta_t.tv_sec == 0 &&
eecb3e4e 375 delta_t.tv_nsec < NSEC_PER_MSEC * 20) {
ac9bbd08 376 pr_err("missed loading, retrying\n");
eecb3e4e
AS
377 pdata->set_dconload(1);
378 mdelay(41);
379 pdata->set_dconload(0);
309ef2a2 380 getnstimeofday(&dcon->load_time);
eecb3e4e
AS
381 mdelay(41);
382 }
383 }
384
45bfe972 385 dcon_blank_fb(dcon, true);
ac9bbd08 386 pr_info("The DCON has control\n");
eecb3e4e
AS
387 break;
388 }
389 default:
390 BUG();
391 }
392
bbe963f1 393 dcon->curr_src = source;
eecb3e4e
AS
394}
395
8d2d3dd1 396static void dcon_set_source(struct dcon_priv *dcon, int arg)
eecb3e4e 397{
bbe963f1 398 if (dcon->pending_src == arg)
eecb3e4e
AS
399 return;
400
bbe963f1 401 dcon->pending_src = arg;
eecb3e4e 402
bbe963f1 403 if ((dcon->curr_src != arg) && !work_pending(&dcon->switch_source))
8d2d3dd1 404 schedule_work(&dcon->switch_source);
eecb3e4e
AS
405}
406
8d2d3dd1 407static void dcon_set_source_sync(struct dcon_priv *dcon, int arg)
eecb3e4e 408{
8d2d3dd1 409 dcon_set_source(dcon, arg);
eecb3e4e
AS
410 flush_scheduled_work();
411}
412
eecb3e4e
AS
413static ssize_t dcon_mode_show(struct device *dev,
414 struct device_attribute *attr, char *buf)
415{
bada46e5
AS
416 struct dcon_priv *dcon = dev_get_drvdata(dev);
417 return sprintf(buf, "%4.4X\n", dcon->disp_mode);
eecb3e4e
AS
418}
419
420static ssize_t dcon_sleep_show(struct device *dev,
421 struct device_attribute *attr, char *buf)
422{
423
bada46e5 424 struct dcon_priv *dcon = dev_get_drvdata(dev);
9ed62423 425 return sprintf(buf, "%d\n", dcon->asleep);
eecb3e4e
AS
426}
427
428static ssize_t dcon_freeze_show(struct device *dev,
429 struct device_attribute *attr, char *buf)
430{
bbe963f1
AS
431 struct dcon_priv *dcon = dev_get_drvdata(dev);
432 return sprintf(buf, "%d\n", dcon->curr_src == DCON_SOURCE_DCON ? 1 : 0);
eecb3e4e
AS
433}
434
bb410354 435static ssize_t dcon_mono_show(struct device *dev,
eecb3e4e
AS
436 struct device_attribute *attr, char *buf)
437{
bb410354 438 struct dcon_priv *dcon = dev_get_drvdata(dev);
9ed62423 439 return sprintf(buf, "%d\n", dcon->mono);
eecb3e4e
AS
440}
441
442static ssize_t dcon_resumeline_show(struct device *dev,
443 struct device_attribute *attr, char *buf)
444{
445 return sprintf(buf, "%d\n", resumeline);
446}
447
bb410354 448static ssize_t dcon_mono_store(struct device *dev,
eecb3e4e
AS
449 struct device_attribute *attr, const char *buf, size_t count)
450{
31a3da41
MB
451 unsigned long enable_mono;
452 int rc;
eecb3e4e 453
88e09a5e 454 rc = kstrtoul(buf, 10, &enable_mono);
31a3da41
MB
455 if (rc)
456 return rc;
eecb3e4e 457
9ed62423 458 dcon_set_mono_mode(dev_get_drvdata(dev), enable_mono ? true : false);
eecb3e4e 459
31a3da41 460 return count;
eecb3e4e
AS
461}
462
463static ssize_t dcon_freeze_store(struct device *dev,
464 struct device_attribute *attr, const char *buf, size_t count)
465{
8d2d3dd1 466 struct dcon_priv *dcon = dev_get_drvdata(dev);
31a3da41
MB
467 unsigned long output;
468 int ret;
eecb3e4e 469
88e09a5e 470 ret = kstrtoul(buf, 10, &output);
31a3da41
MB
471 if (ret)
472 return ret;
eecb3e4e 473
ac9bbd08 474 pr_info("dcon_freeze_store: %lu\n", output);
eecb3e4e
AS
475
476 switch (output) {
477 case 0:
8d2d3dd1 478 dcon_set_source(dcon, DCON_SOURCE_CPU);
eecb3e4e
AS
479 break;
480 case 1:
8d2d3dd1 481 dcon_set_source_sync(dcon, DCON_SOURCE_DCON);
eecb3e4e 482 break;
e107e6eb 483 case 2: /* normally unused */
8d2d3dd1 484 dcon_set_source(dcon, DCON_SOURCE_DCON);
eecb3e4e
AS
485 break;
486 default:
487 return -EINVAL;
488 }
489
490 return count;
491}
492
493static ssize_t dcon_resumeline_store(struct device *dev,
494 struct device_attribute *attr, const char *buf, size_t count)
495{
c542341d 496 unsigned short rl;
31a3da41 497 int rc;
eecb3e4e 498
c542341d 499 rc = kstrtou16(buf, 10, &rl);
31a3da41 500 if (rc)
eecb3e4e
AS
501 return rc;
502
503 resumeline = rl;
8d2d3dd1 504 dcon_write(dev_get_drvdata(dev), DCON_REG_SCAN_INT, resumeline);
eecb3e4e 505
31a3da41 506 return count;
eecb3e4e
AS
507}
508
509static ssize_t dcon_sleep_store(struct device *dev,
510 struct device_attribute *attr, const char *buf, size_t count)
511{
31a3da41
MB
512 unsigned long output;
513 int ret;
eecb3e4e 514
88e09a5e 515 ret = kstrtoul(buf, 10, &output);
31a3da41
MB
516 if (ret)
517 return ret;
eecb3e4e 518
bada46e5 519 dcon_sleep(dev_get_drvdata(dev), output ? true : false);
eecb3e4e
AS
520 return count;
521}
522
523static struct device_attribute dcon_device_files[] = {
524 __ATTR(mode, 0444, dcon_mode_show, NULL),
525 __ATTR(sleep, 0644, dcon_sleep_show, dcon_sleep_store),
526 __ATTR(freeze, 0644, dcon_freeze_show, dcon_freeze_store),
bb410354 527 __ATTR(monochrome, 0644, dcon_mono_show, dcon_mono_store),
eecb3e4e
AS
528 __ATTR(resumeline, 0644, dcon_resumeline_show, dcon_resumeline_store),
529};
530
c59eef17
AS
531static int dcon_bl_update(struct backlight_device *dev)
532{
533 struct dcon_priv *dcon = bl_get_data(dev);
534 u8 level = dev->props.brightness & 0x0F;
535
536 if (dev->props.power != FB_BLANK_UNBLANK)
537 level = 0;
538
539 if (level != dcon->bl_val)
540 dcon_set_backlight(dcon, level);
541
542 return 0;
543}
544
545static int dcon_bl_get(struct backlight_device *dev)
546{
547 struct dcon_priv *dcon = bl_get_data(dev);
548 return dcon->bl_val;
549}
550
acc2472e 551static const struct backlight_ops dcon_bl_ops = {
c59eef17
AS
552 .update_status = dcon_bl_update,
553 .get_brightness = dcon_bl_get,
eecb3e4e
AS
554};
555
c59eef17
AS
556static struct backlight_properties dcon_bl_props = {
557 .max_brightness = 15,
bb7ca747 558 .type = BACKLIGHT_RAW,
c59eef17
AS
559 .power = FB_BLANK_UNBLANK,
560};
eecb3e4e 561
e107e6eb
MB
562static int dcon_reboot_notify(struct notifier_block *nb,
563 unsigned long foo, void *bar)
eecb3e4e 564{
8d2d3dd1
AS
565 struct dcon_priv *dcon = container_of(nb, struct dcon_priv, reboot_nb);
566
567 if (!dcon || !dcon->client)
eecb3e4e
AS
568 return 0;
569
570 /* Turn off the DCON. Entirely. */
8d2d3dd1
AS
571 dcon_write(dcon, DCON_REG_MODE, 0x39);
572 dcon_write(dcon, DCON_REG_MODE, 0x32);
eecb3e4e
AS
573 return 0;
574}
575
e107e6eb
MB
576static int unfreeze_on_panic(struct notifier_block *nb,
577 unsigned long e, void *p)
eecb3e4e
AS
578{
579 pdata->set_dconload(1);
580 return NOTIFY_DONE;
581}
582
583static struct notifier_block dcon_panic_nb = {
584 .notifier_call = unfreeze_on_panic,
585};
586
95699fb7
AS
587/*
588 * When the framebuffer sleeps due to external sources (e.g. user idle), power
589 * down the DCON as well. Power it back up when the fb comes back to life.
590 */
feaa98b2 591static int dcon_fb_notifier(struct notifier_block *self,
e107e6eb 592 unsigned long event, void *data)
eecb3e4e
AS
593{
594 struct fb_event *evdata = data;
feaa98b2
AS
595 struct dcon_priv *dcon = container_of(self, struct dcon_priv,
596 fbevent_nb);
c2562687 597 int *blank = (int *)evdata->data;
95699fb7 598 if (((event != FB_EVENT_BLANK) && (event != FB_EVENT_CONBLANK)) ||
45bfe972 599 dcon->ignore_fb_events)
eecb3e4e 600 return 0;
bada46e5 601 dcon_sleep(dcon, *blank ? true : false);
eecb3e4e
AS
602 return 0;
603}
604
eecb3e4e
AS
605static int dcon_detect(struct i2c_client *client, struct i2c_board_info *info)
606{
607 strlcpy(info->type, "olpc_dcon", I2C_NAME_SIZE);
608
609 return 0;
610}
611
612static int dcon_probe(struct i2c_client *client, const struct i2c_device_id *id)
613{
8d2d3dd1 614 struct dcon_priv *dcon;
a90dcd4f 615 int rc, i, j;
eecb3e4e 616
097cd83a
AS
617 if (!pdata)
618 return -ENXIO;
619
8d2d3dd1
AS
620 dcon = kzalloc(sizeof(*dcon), GFP_KERNEL);
621 if (!dcon)
622 return -ENOMEM;
623
624 dcon->client = client;
625 INIT_WORK(&dcon->switch_source, dcon_source_switch);
626 dcon->reboot_nb.notifier_call = dcon_reboot_notify;
627 dcon->reboot_nb.priority = -1;
feaa98b2 628 dcon->fbevent_nb.notifier_call = dcon_fb_notifier;
8d2d3dd1
AS
629
630 i2c_set_clientdata(client, dcon);
631
45bfe972
AS
632 if (num_registered_fb < 1) {
633 dev_err(&client->dev, "DCON driver requires a registered fb\n");
634 rc = -EIO;
635 goto einit;
636 }
637 dcon->fbinfo = registered_fb[0];
eecb3e4e 638
8d2d3dd1 639 rc = dcon_hw_init(dcon, 1);
eecb3e4e
AS
640 if (rc)
641 goto einit;
642
643 /* Add the DCON device */
644
645 dcon_device = platform_device_alloc("dcon", -1);
646
647 if (dcon_device == NULL) {
ac9bbd08 648 pr_err("Unable to create the DCON device\n");
eecb3e4e
AS
649 rc = -ENOMEM;
650 goto eirq;
651 }
e107e6eb 652 rc = platform_device_add(dcon_device);
8d2d3dd1 653 platform_set_drvdata(dcon_device, dcon);
eecb3e4e 654
e107e6eb 655 if (rc) {
ac9bbd08 656 pr_err("Unable to add the DCON device\n");
eecb3e4e
AS
657 goto edev;
658 }
659
e107e6eb 660 for (i = 0; i < ARRAY_SIZE(dcon_device_files); i++) {
a90dcd4f
MB
661 rc = device_create_file(&dcon_device->dev,
662 &dcon_device_files[i]);
663 if (rc) {
664 dev_err(&dcon_device->dev, "Cannot create sysfs file\n");
665 goto ecreate;
666 }
667 }
eecb3e4e 668
c59eef17 669 dcon->bl_val = dcon_read(dcon, DCON_REG_BRIGHT) & 0x0F;
eecb3e4e 670
c59eef17
AS
671 /* Add the backlight device for the DCON */
672 dcon_bl_props.brightness = dcon->bl_val;
673 dcon->bl_dev = backlight_device_register("dcon-bl", &dcon_device->dev,
674 dcon, &dcon_bl_ops, &dcon_bl_props);
675 if (IS_ERR(dcon->bl_dev)) {
676 dev_err(&client->dev, "cannot register backlight dev (%ld)\n",
677 PTR_ERR(dcon->bl_dev));
678 dcon->bl_dev = NULL;
eecb3e4e
AS
679 }
680
8d2d3dd1 681 register_reboot_notifier(&dcon->reboot_nb);
eecb3e4e 682 atomic_notifier_chain_register(&panic_notifier_list, &dcon_panic_nb);
feaa98b2 683 fb_register_client(&dcon->fbevent_nb);
eecb3e4e
AS
684
685 return 0;
686
a90dcd4f
MB
687 ecreate:
688 for (j = 0; j < i; j++)
689 device_remove_file(&dcon_device->dev, &dcon_device_files[j]);
eecb3e4e
AS
690 edev:
691 platform_device_unregister(dcon_device);
692 dcon_device = NULL;
eecb3e4e 693 eirq:
bbe963f1 694 free_irq(DCON_IRQ, dcon);
eecb3e4e 695 einit:
8d2d3dd1 696 kfree(dcon);
eecb3e4e
AS
697 return rc;
698}
699
700static int dcon_remove(struct i2c_client *client)
701{
8d2d3dd1
AS
702 struct dcon_priv *dcon = i2c_get_clientdata(client);
703
feaa98b2 704 fb_unregister_client(&dcon->fbevent_nb);
8d2d3dd1 705 unregister_reboot_notifier(&dcon->reboot_nb);
eecb3e4e
AS
706 atomic_notifier_chain_unregister(&panic_notifier_list, &dcon_panic_nb);
707
bbe963f1 708 free_irq(DCON_IRQ, dcon);
eecb3e4e 709
c59eef17
AS
710 if (dcon->bl_dev)
711 backlight_device_unregister(dcon->bl_dev);
eecb3e4e
AS
712
713 if (dcon_device != NULL)
714 platform_device_unregister(dcon_device);
8d2d3dd1
AS
715 cancel_work_sync(&dcon->switch_source);
716
717 kfree(dcon);
eecb3e4e 718
eecb3e4e
AS
719 return 0;
720}
721
722#ifdef CONFIG_PM
723static int dcon_suspend(struct i2c_client *client, pm_message_t state)
724{
8d2d3dd1
AS
725 struct dcon_priv *dcon = i2c_get_clientdata(client);
726
bada46e5 727 if (!dcon->asleep) {
eecb3e4e 728 /* Set up the DCON to have the source */
8d2d3dd1 729 dcon_set_source_sync(dcon, DCON_SOURCE_DCON);
eecb3e4e
AS
730 }
731
732 return 0;
733}
734
735static int dcon_resume(struct i2c_client *client)
736{
8d2d3dd1
AS
737 struct dcon_priv *dcon = i2c_get_clientdata(client);
738
bada46e5 739 if (!dcon->asleep) {
8d2d3dd1
AS
740 dcon_bus_stabilize(dcon, 0);
741 dcon_set_source(dcon, DCON_SOURCE_CPU);
eecb3e4e
AS
742 }
743
744 return 0;
745}
746
747#endif
748
749
097cd83a 750irqreturn_t dcon_interrupt(int irq, void *id)
eecb3e4e 751{
bbe963f1 752 struct dcon_priv *dcon = id;
91762057 753 u8 status;
eecb3e4e 754
91762057 755 if (pdata->read_status(&status))
eecb3e4e
AS
756 return IRQ_NONE;
757
758 switch (status & 3) {
759 case 3:
ac9bbd08 760 pr_debug("DCONLOAD_MISSED interrupt\n");
eecb3e4e
AS
761 break;
762
763 case 2: /* switch to DCON mode */
764 case 1: /* switch to CPU mode */
309ef2a2
AS
765 dcon->switched = true;
766 getnstimeofday(&dcon->irq_time);
eecb3e4e
AS
767 wake_up(&dcon_wait_queue);
768 break;
769
770 case 0:
771 /* workaround resume case: the DCON (on 1.5) doesn't
772 * ever assert status 0x01 when switching to CPU mode
773 * during resume. this is because DCONLOAD is de-asserted
774 * _immediately_ upon exiting S3, so the actual release
775 * of the DCON happened long before this point.
776 * see http://dev.laptop.org/ticket/9869
777 */
309ef2a2
AS
778 if (dcon->curr_src != dcon->pending_src && !dcon->switched) {
779 dcon->switched = true;
780 getnstimeofday(&dcon->irq_time);
eecb3e4e 781 wake_up(&dcon_wait_queue);
ac9bbd08 782 pr_debug("switching w/ status 0/0\n");
eecb3e4e 783 } else {
ac9bbd08 784 pr_debug("scanline interrupt w/CPU\n");
eecb3e4e
AS
785 }
786 }
787
788 return IRQ_HANDLED;
789}
790
56463de0 791static const struct i2c_device_id dcon_idtable[] = {
eecb3e4e
AS
792 { "olpc_dcon", 0 },
793 { }
794};
795
796MODULE_DEVICE_TABLE(i2c, dcon_idtable);
797
097cd83a 798struct i2c_driver dcon_driver = {
eecb3e4e
AS
799 .driver = {
800 .name = "olpc_dcon",
801 },
802 .class = I2C_CLASS_DDC | I2C_CLASS_HWMON,
803 .id_table = dcon_idtable,
804 .probe = dcon_probe,
805 .remove = __devexit_p(dcon_remove),
806 .detect = dcon_detect,
807 .address_list = normal_i2c,
808#ifdef CONFIG_PM
809 .suspend = dcon_suspend,
810 .resume = dcon_resume,
811#endif
812};
813
eecb3e4e
AS
814static int __init olpc_dcon_init(void)
815{
097cd83a
AS
816#ifdef CONFIG_FB_OLPC_DCON_1_5
817 /* XO-1.5 */
818 if (olpc_board_at_least(olpc_board(0xd0)))
819 pdata = &dcon_pdata_xo_1_5;
820#endif
821#ifdef CONFIG_FB_OLPC_DCON_1
822 if (!pdata)
823 pdata = &dcon_pdata_xo_1;
824#endif
eecb3e4e 825
56463de0 826 return i2c_add_driver(&dcon_driver);
eecb3e4e
AS
827}
828
829static void __exit olpc_dcon_exit(void)
830{
831 i2c_del_driver(&dcon_driver);
832}
833
834module_init(olpc_dcon_init);
835module_exit(olpc_dcon_exit);
836
837MODULE_LICENSE("GPL");