Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 1 | /* |
| 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 19 | #include <linux/gpio.h> |
| 20 | #include <linux/delay.h> |
| 21 | #include <linux/lcd.h> |
| 22 | #include <linux/fb.h> |
| 23 | |
| 24 | #include <asm/mach/sharpsl_param.h> |
| 25 | |
| 26 | #include <mach/tosa.h> |
| 27 | |
| 28 | #define POWER_IS_ON(pwr) ((pwr) <= FB_BLANK_NORMAL) |
| 29 | |
| 30 | #define TG_REG0_VQV 0x0001 |
| 31 | #define TG_REG0_COLOR 0x0002 |
| 32 | #define TG_REG0_UD 0x0004 |
| 33 | #define TG_REG0_LR 0x0008 |
| 34 | |
| 35 | #define DAC_BASE 0x4e |
| 36 | |
| 37 | struct tosa_lcd_data { |
| 38 | struct spi_device *spi; |
| 39 | struct lcd_device *lcd; |
| 40 | struct i2c_client *i2c; |
| 41 | |
| 42 | int lcd_power; |
Dmitry Baryshkov | 0ec561f | 2008-12-04 16:54:42 +0000 | [diff] [blame] | 43 | bool is_vga; |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | static int tosa_tg_send(struct spi_device *spi, int adrs, uint8_t data) |
| 47 | { |
| 48 | u8 buf[1]; |
| 49 | struct spi_message msg; |
| 50 | struct spi_transfer xfer = { |
| 51 | .len = 1, |
| 52 | .cs_change = 1, |
| 53 | .tx_buf = buf, |
| 54 | }; |
| 55 | |
| 56 | buf[0] = ((adrs & 0x07) << 5) | (data & 0x1f); |
| 57 | spi_message_init(&msg); |
| 58 | spi_message_add_tail(&xfer, &msg); |
| 59 | |
| 60 | return spi_sync(spi, &msg); |
| 61 | } |
| 62 | |
| 63 | int tosa_bl_enable(struct spi_device *spi, int enable) |
| 64 | { |
| 65 | /* bl_enable GP04=1 otherwise GP04=0*/ |
Jingoo Han | 8ab77f4 | 2012-12-17 16:00:36 -0800 | [diff] [blame] | 66 | return tosa_tg_send(spi, TG_GPODR2, enable ? 0x01 : 0x00); |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 67 | } |
| 68 | EXPORT_SYMBOL(tosa_bl_enable); |
| 69 | |
| 70 | static void tosa_lcd_tg_init(struct tosa_lcd_data *data) |
| 71 | { |
| 72 | /* TG on */ |
| 73 | gpio_set_value(TOSA_GPIO_TG_ON, 0); |
| 74 | |
| 75 | mdelay(60); |
| 76 | |
| 77 | /* delayed 0clk TCTL signal for VGA */ |
| 78 | tosa_tg_send(data->spi, TG_TPOSCTL, 0x00); |
| 79 | /* GPOS0=powercontrol, GPOS1=GPIO, GPOS2=TCTL */ |
| 80 | tosa_tg_send(data->spi, TG_GPOSR, 0x02); |
| 81 | } |
| 82 | |
| 83 | static void tosa_lcd_tg_on(struct tosa_lcd_data *data) |
| 84 | { |
| 85 | struct spi_device *spi = data->spi; |
Dmitry Baryshkov | 0ec561f | 2008-12-04 16:54:42 +0000 | [diff] [blame] | 86 | int value = TG_REG0_COLOR | TG_REG0_UD | TG_REG0_LR; |
| 87 | |
| 88 | if (data->is_vga) |
| 89 | value |= TG_REG0_VQV; |
| 90 | |
| 91 | tosa_tg_send(spi, TG_PNLCTL, value); |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 92 | |
| 93 | /* TG LCD pannel power up */ |
Jingoo Han | 8ab77f4 | 2012-12-17 16:00:36 -0800 | [diff] [blame] | 94 | tosa_tg_send(spi, TG_PINICTL, 0x4); |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 95 | mdelay(50); |
| 96 | |
| 97 | /* TG LCD GVSS */ |
Jingoo Han | 8ab77f4 | 2012-12-17 16:00:36 -0800 | [diff] [blame] | 98 | tosa_tg_send(spi, TG_PINICTL, 0x0); |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 99 | |
| 100 | if (!data->i2c) { |
Jingoo Han | 8ab77f4 | 2012-12-17 16:00:36 -0800 | [diff] [blame] | 101 | /* |
| 102 | * after the pannel is powered up the first time, |
| 103 | * we can access the i2c bus so probe for the DAC |
| 104 | */ |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 105 | struct i2c_adapter *adap = i2c_get_adapter(0); |
| 106 | struct i2c_board_info info = { |
| 107 | .type = "tosa-bl", |
| 108 | .addr = DAC_BASE, |
| 109 | .platform_data = data->spi, |
| 110 | }; |
| 111 | data->i2c = i2c_new_device(adap, &info); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | static void tosa_lcd_tg_off(struct tosa_lcd_data *data) |
| 116 | { |
| 117 | struct spi_device *spi = data->spi; |
| 118 | |
| 119 | /* TG LCD VHSA off */ |
Jingoo Han | 8ab77f4 | 2012-12-17 16:00:36 -0800 | [diff] [blame] | 120 | tosa_tg_send(spi, TG_PINICTL, 0x4); |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 121 | mdelay(50); |
| 122 | |
| 123 | /* TG LCD signal off */ |
Jingoo Han | 8ab77f4 | 2012-12-17 16:00:36 -0800 | [diff] [blame] | 124 | tosa_tg_send(spi, TG_PINICTL, 0x6); |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 125 | mdelay(50); |
| 126 | |
| 127 | /* TG Off */ |
| 128 | gpio_set_value(TOSA_GPIO_TG_ON, 1); |
| 129 | mdelay(100); |
| 130 | } |
| 131 | |
| 132 | int tosa_lcd_set_power(struct lcd_device *lcd, int power) |
| 133 | { |
| 134 | struct tosa_lcd_data *data = lcd_get_data(lcd); |
| 135 | |
| 136 | if (POWER_IS_ON(power) && !POWER_IS_ON(data->lcd_power)) |
| 137 | tosa_lcd_tg_on(data); |
| 138 | |
| 139 | if (!POWER_IS_ON(power) && POWER_IS_ON(data->lcd_power)) |
| 140 | tosa_lcd_tg_off(data); |
| 141 | |
| 142 | data->lcd_power = power; |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | static int tosa_lcd_get_power(struct lcd_device *lcd) |
| 147 | { |
| 148 | struct tosa_lcd_data *data = lcd_get_data(lcd); |
| 149 | |
| 150 | return data->lcd_power; |
| 151 | } |
| 152 | |
Dmitry Baryshkov | 0ec561f | 2008-12-04 16:54:42 +0000 | [diff] [blame] | 153 | static int tosa_lcd_set_mode(struct lcd_device *lcd, struct fb_videomode *mode) |
| 154 | { |
| 155 | struct tosa_lcd_data *data = lcd_get_data(lcd); |
| 156 | |
| 157 | if (mode->xres == 320 || mode->yres == 320) |
| 158 | data->is_vga = false; |
| 159 | else |
| 160 | data->is_vga = true; |
| 161 | |
| 162 | if (POWER_IS_ON(data->lcd_power)) |
| 163 | tosa_lcd_tg_on(data); |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 168 | static struct lcd_ops tosa_lcd_ops = { |
| 169 | .set_power = tosa_lcd_set_power, |
| 170 | .get_power = tosa_lcd_get_power, |
Dmitry Baryshkov | 0ec561f | 2008-12-04 16:54:42 +0000 | [diff] [blame] | 171 | .set_mode = tosa_lcd_set_mode, |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 172 | }; |
| 173 | |
Bill Pemberton | 1b9e450 | 2012-11-19 13:21:46 -0500 | [diff] [blame] | 174 | static int tosa_lcd_probe(struct spi_device *spi) |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 175 | { |
| 176 | int ret; |
| 177 | struct tosa_lcd_data *data; |
| 178 | |
Jingoo Han | c851529 | 2012-05-29 15:07:25 -0700 | [diff] [blame] | 179 | data = devm_kzalloc(&spi->dev, sizeof(struct tosa_lcd_data), |
| 180 | GFP_KERNEL); |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 181 | if (!data) |
| 182 | return -ENOMEM; |
| 183 | |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 184 | data->is_vga = true; /* default to VGA mode */ |
Dmitry Baryshkov | 0ec561f | 2008-12-04 16:54:42 +0000 | [diff] [blame] | 185 | |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 186 | /* |
| 187 | * bits_per_word cannot be configured in platform data |
| 188 | */ |
| 189 | spi->bits_per_word = 8; |
| 190 | |
| 191 | ret = spi_setup(spi); |
| 192 | if (ret < 0) |
Jingoo Han | c851529 | 2012-05-29 15:07:25 -0700 | [diff] [blame] | 193 | return ret; |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 194 | |
| 195 | data->spi = spi; |
Jingoo Han | c324f51 | 2013-02-21 16:43:41 -0800 | [diff] [blame] | 196 | spi_set_drvdata(spi, data); |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 197 | |
Jingoo Han | 05a5d4d | 2012-12-17 16:00:41 -0800 | [diff] [blame] | 198 | ret = devm_gpio_request_one(&spi->dev, TOSA_GPIO_TG_ON, |
| 199 | GPIOF_OUT_INIT_LOW, "tg #pwr"); |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 200 | if (ret < 0) |
Sachin Kamat | d24c815 | 2013-09-20 12:08:53 +0530 | [diff] [blame] | 201 | return ret; |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 202 | |
| 203 | mdelay(60); |
| 204 | |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 205 | tosa_lcd_tg_init(data); |
| 206 | |
| 207 | tosa_lcd_tg_on(data); |
| 208 | |
Jingoo Han | 0f53449 | 2014-01-23 15:54:30 -0800 | [diff] [blame] | 209 | data->lcd = devm_lcd_device_register(&spi->dev, "tosa-lcd", &spi->dev, |
| 210 | data, &tosa_lcd_ops); |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 211 | |
| 212 | if (IS_ERR(data->lcd)) { |
| 213 | ret = PTR_ERR(data->lcd); |
| 214 | data->lcd = NULL; |
| 215 | goto err_register; |
| 216 | } |
| 217 | |
| 218 | return 0; |
| 219 | |
| 220 | err_register: |
| 221 | tosa_lcd_tg_off(data); |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 222 | return ret; |
| 223 | } |
| 224 | |
Bill Pemberton | 7e4b9d0 | 2012-11-19 13:26:34 -0500 | [diff] [blame] | 225 | static int tosa_lcd_remove(struct spi_device *spi) |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 226 | { |
Jingoo Han | c324f51 | 2013-02-21 16:43:41 -0800 | [diff] [blame] | 227 | struct tosa_lcd_data *data = spi_get_drvdata(spi); |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 228 | |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 229 | if (data->i2c) |
| 230 | i2c_unregister_device(data->i2c); |
| 231 | |
| 232 | tosa_lcd_tg_off(data); |
| 233 | |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 234 | return 0; |
| 235 | } |
| 236 | |
Jingoo Han | 1dde37d | 2013-04-29 16:18:00 -0700 | [diff] [blame] | 237 | #ifdef CONFIG_PM_SLEEP |
| 238 | static int tosa_lcd_suspend(struct device *dev) |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 239 | { |
Jingoo Han | 1dde37d | 2013-04-29 16:18:00 -0700 | [diff] [blame] | 240 | struct tosa_lcd_data *data = dev_get_drvdata(dev); |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 241 | |
| 242 | tosa_lcd_tg_off(data); |
| 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | |
Jingoo Han | 1dde37d | 2013-04-29 16:18:00 -0700 | [diff] [blame] | 247 | static int tosa_lcd_resume(struct device *dev) |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 248 | { |
Jingoo Han | 1dde37d | 2013-04-29 16:18:00 -0700 | [diff] [blame] | 249 | struct tosa_lcd_data *data = dev_get_drvdata(dev); |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 250 | |
| 251 | tosa_lcd_tg_init(data); |
| 252 | if (POWER_IS_ON(data->lcd_power)) |
| 253 | tosa_lcd_tg_on(data); |
| 254 | else |
| 255 | tosa_lcd_tg_off(data); |
| 256 | |
| 257 | return 0; |
| 258 | } |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 259 | #endif |
| 260 | |
Jingoo Han | 1dde37d | 2013-04-29 16:18:00 -0700 | [diff] [blame] | 261 | static SIMPLE_DEV_PM_OPS(tosa_lcd_pm_ops, tosa_lcd_suspend, tosa_lcd_resume); |
| 262 | |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 263 | static struct spi_driver tosa_lcd_driver = { |
| 264 | .driver = { |
| 265 | .name = "tosa-lcd", |
| 266 | .owner = THIS_MODULE, |
Jingoo Han | 1dde37d | 2013-04-29 16:18:00 -0700 | [diff] [blame] | 267 | .pm = &tosa_lcd_pm_ops, |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 268 | }, |
| 269 | .probe = tosa_lcd_probe, |
Bill Pemberton | d1723fa | 2012-11-19 13:21:09 -0500 | [diff] [blame] | 270 | .remove = tosa_lcd_remove, |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 271 | }; |
| 272 | |
Axel Lin | 462dd83 | 2012-03-23 15:01:59 -0700 | [diff] [blame] | 273 | module_spi_driver(tosa_lcd_driver); |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 274 | |
| 275 | MODULE_AUTHOR("Dmitry Baryshkov"); |
| 276 | MODULE_LICENSE("GPL v2"); |
| 277 | MODULE_DESCRIPTION("LCD/Backlight control for Sharp SL-6000 PDA"); |
Anton Vorontsov | e0626e3 | 2009-09-22 16:46:08 -0700 | [diff] [blame] | 278 | MODULE_ALIAS("spi:tosa-lcd"); |