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