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