blob: 83742d8063918d46e4463acabd5e38672abddb8c [file] [log] [blame]
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +04001/*
2 * LCD / Backlight control code for Sharp SL-6000x (tosa)
3 *
4 * Copyright (c) 2005 Dirk Opfer
5 * Copyright (c) 2007,2008 Dmitry Baryshkov
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 */
12
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/device.h>
16#include <linux/spi/spi.h>
17#include <linux/i2c.h>
18#include <linux/gpio.h>
19#include <linux/fb.h>
20#include <linux/backlight.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +040022
23#include <asm/mach/sharpsl_param.h>
24
25#include <mach/tosa.h>
26
27#define COMADJ_DEFAULT 97
28
29#define DAC_CH1 0
30#define DAC_CH2 1
31
32struct tosa_bl_data {
33 struct i2c_client *i2c;
34 struct backlight_device *bl;
35
36 int comadj;
37};
38
39static void tosa_bl_set_backlight(struct tosa_bl_data *data, int brightness)
40{
Jingoo Hanc512794c2013-11-12 15:09:04 -080041 struct spi_device *spi = dev_get_platdata(&data->i2c->dev);
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +040042
43 i2c_smbus_write_byte_data(data->i2c, DAC_CH1, data->comadj);
44
45 /* SetBacklightDuty */
46 i2c_smbus_write_byte_data(data->i2c, DAC_CH2, (u8)(brightness & 0xff));
47
48 /* SetBacklightVR */
49 gpio_set_value(TOSA_GPIO_BL_C20MA, brightness & 0x100);
50
51 tosa_bl_enable(spi, brightness);
52}
53
54static int tosa_bl_update_status(struct backlight_device *dev)
55{
56 struct backlight_properties *props = &dev->props;
Jingoo Han42109b32013-02-21 16:43:49 -080057 struct tosa_bl_data *data = bl_get_data(dev);
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +040058 int power = max(props->power, props->fb_blank);
59 int brightness = props->brightness;
60
61 if (power)
62 brightness = 0;
63
64 tosa_bl_set_backlight(data, brightness);
65
66 return 0;
67}
68
69static int tosa_bl_get_brightness(struct backlight_device *dev)
70{
71 struct backlight_properties *props = &dev->props;
72
73 return props->brightness;
74}
75
Emese Revfy9905a432009-12-14 00:58:57 +010076static const struct backlight_ops bl_ops = {
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +040077 .get_brightness = tosa_bl_get_brightness,
78 .update_status = tosa_bl_update_status,
79};
80
Bill Pemberton1b9e4502012-11-19 13:21:46 -050081static int tosa_bl_probe(struct i2c_client *client,
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +040082 const struct i2c_device_id *id)
83{
Matthew Garretta19a6ee2010-02-17 16:39:44 -050084 struct backlight_properties props;
Jingoo Hanf072c892012-05-29 15:07:25 -070085 struct tosa_bl_data *data;
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +040086 int ret = 0;
Jingoo Hanf072c892012-05-29 15:07:25 -070087
88 data = devm_kzalloc(&client->dev, sizeof(struct tosa_bl_data),
89 GFP_KERNEL);
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +040090 if (!data)
91 return -ENOMEM;
92
93 data->comadj = sharpsl_param.comadj == -1 ? COMADJ_DEFAULT : sharpsl_param.comadj;
94
Jingoo Han05a5d4d2012-12-17 16:00:41 -080095 ret = devm_gpio_request_one(&client->dev, TOSA_GPIO_BL_C20MA,
96 GPIOF_OUT_INIT_LOW, "backlight");
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +040097 if (ret) {
98 dev_dbg(&data->bl->dev, "Unable to request gpio!\n");
Jingoo Hanf072c892012-05-29 15:07:25 -070099 return ret;
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +0400100 }
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +0400101
102 i2c_set_clientdata(client, data);
103 data->i2c = client;
104
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500105 memset(&props, 0, sizeof(struct backlight_properties));
Matthew Garrettbb7ca742011-03-22 16:30:21 -0700106 props.type = BACKLIGHT_RAW;
Matthew Garretta19a6ee2010-02-17 16:39:44 -0500107 props.max_brightness = 512 - 1;
Jingoo Hanffb1f6c2014-01-23 15:54:28 -0800108 data->bl = devm_backlight_device_register(&client->dev, "tosa-bl",
109 &client->dev, data, &bl_ops,
110 &props);
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +0400111 if (IS_ERR(data->bl)) {
112 ret = PTR_ERR(data->bl);
113 goto err_reg;
114 }
115
116 data->bl->props.brightness = 69;
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +0400117 data->bl->props.power = FB_BLANK_UNBLANK;
118
119 backlight_update_status(data->bl);
120
121 return 0;
122
123err_reg:
124 data->bl = NULL;
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +0400125 return ret;
126}
127
Bill Pemberton7e4b9d02012-11-19 13:26:34 -0500128static int tosa_bl_remove(struct i2c_client *client)
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +0400129{
130 struct tosa_bl_data *data = i2c_get_clientdata(client);
131
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +0400132 data->bl = NULL;
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +0400133 return 0;
134}
135
Jingoo Han1dde37d2013-04-29 16:18:00 -0700136#ifdef CONFIG_PM_SLEEP
137static int tosa_bl_suspend(struct device *dev)
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +0400138{
Jingoo Han1dde37d2013-04-29 16:18:00 -0700139 struct tosa_bl_data *data = dev_get_drvdata(dev);
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +0400140
141 tosa_bl_set_backlight(data, 0);
142
143 return 0;
144}
145
Jingoo Han1dde37d2013-04-29 16:18:00 -0700146static int tosa_bl_resume(struct device *dev)
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +0400147{
Jingoo Han1dde37d2013-04-29 16:18:00 -0700148 struct tosa_bl_data *data = dev_get_drvdata(dev);
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +0400149
150 backlight_update_status(data->bl);
151 return 0;
152}
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +0400153#endif
154
Jingoo Han1dde37d2013-04-29 16:18:00 -0700155static SIMPLE_DEV_PM_OPS(tosa_bl_pm_ops, tosa_bl_suspend, tosa_bl_resume);
156
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +0400157static const struct i2c_device_id tosa_bl_id[] = {
158 { "tosa-bl", 0 },
159 { },
160};
Javier Martinez Canillas13d20b32015-08-25 08:31:12 +0200161MODULE_DEVICE_TABLE(i2c, tosa_bl_id);
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +0400162
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +0400163static struct i2c_driver tosa_bl_driver = {
164 .driver = {
165 .name = "tosa-bl",
166 .owner = THIS_MODULE,
Jingoo Han1dde37d2013-04-29 16:18:00 -0700167 .pm = &tosa_bl_pm_ops,
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +0400168 },
169 .probe = tosa_bl_probe,
Bill Pembertond1723fa2012-11-19 13:21:09 -0500170 .remove = tosa_bl_remove,
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +0400171 .id_table = tosa_bl_id,
172};
173
Axel Lin81ce6862012-03-23 15:01:59 -0700174module_i2c_driver(tosa_bl_driver);
Dmitry Baryshkovfbd1b172008-09-13 20:58:51 +0400175
176MODULE_AUTHOR("Dmitry Baryshkov");
177MODULE_LICENSE("GPL v2");
178MODULE_DESCRIPTION("LCD/Backlight control for Sharp SL-6000 PDA");
179