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> |
| 18 | #include <linux/gpio.h> |
| 19 | #include <linux/fb.h> |
| 20 | #include <linux/backlight.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 21 | #include <linux/slab.h> |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 22 | |
| 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 | |
| 32 | struct tosa_bl_data { |
| 33 | struct i2c_client *i2c; |
| 34 | struct backlight_device *bl; |
| 35 | |
| 36 | int comadj; |
| 37 | }; |
| 38 | |
| 39 | static void tosa_bl_set_backlight(struct tosa_bl_data *data, int brightness) |
| 40 | { |
| 41 | struct spi_device *spi = data->i2c->dev.platform_data; |
| 42 | |
| 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 | |
| 54 | static int tosa_bl_update_status(struct backlight_device *dev) |
| 55 | { |
| 56 | struct backlight_properties *props = &dev->props; |
| 57 | struct tosa_bl_data *data = dev_get_drvdata(&dev->dev); |
| 58 | 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 | |
| 69 | static 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 Revfy | 9905a43 | 2009-12-14 00:58:57 +0100 | [diff] [blame] | 76 | static const struct backlight_ops bl_ops = { |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 77 | .get_brightness = tosa_bl_get_brightness, |
| 78 | .update_status = tosa_bl_update_status, |
| 79 | }; |
| 80 | |
Bill Pemberton | 1b9e450 | 2012-11-19 13:21:46 -0500 | [diff] [blame] | 81 | static int tosa_bl_probe(struct i2c_client *client, |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 82 | const struct i2c_device_id *id) |
| 83 | { |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 84 | struct backlight_properties props; |
Jingoo Han | f072c89 | 2012-05-29 15:07:25 -0700 | [diff] [blame] | 85 | struct tosa_bl_data *data; |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 86 | int ret = 0; |
Jingoo Han | f072c89 | 2012-05-29 15:07:25 -0700 | [diff] [blame] | 87 | |
| 88 | data = devm_kzalloc(&client->dev, sizeof(struct tosa_bl_data), |
| 89 | GFP_KERNEL); |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 90 | if (!data) |
| 91 | return -ENOMEM; |
| 92 | |
| 93 | data->comadj = sharpsl_param.comadj == -1 ? COMADJ_DEFAULT : sharpsl_param.comadj; |
| 94 | |
Jingoo Han | f5b7194 | 2012-07-30 14:40:40 -0700 | [diff] [blame] | 95 | ret = devm_gpio_request(&client->dev, TOSA_GPIO_BL_C20MA, "backlight"); |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 96 | if (ret) { |
| 97 | dev_dbg(&data->bl->dev, "Unable to request gpio!\n"); |
Jingoo Han | f072c89 | 2012-05-29 15:07:25 -0700 | [diff] [blame] | 98 | return ret; |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 99 | } |
| 100 | ret = gpio_direction_output(TOSA_GPIO_BL_C20MA, 0); |
| 101 | if (ret) |
Jingoo Han | f5b7194 | 2012-07-30 14:40:40 -0700 | [diff] [blame] | 102 | return ret; |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 103 | |
| 104 | i2c_set_clientdata(client, data); |
| 105 | data->i2c = client; |
| 106 | |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 107 | memset(&props, 0, sizeof(struct backlight_properties)); |
Matthew Garrett | bb7ca74 | 2011-03-22 16:30:21 -0700 | [diff] [blame] | 108 | props.type = BACKLIGHT_RAW; |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 109 | props.max_brightness = 512 - 1; |
| 110 | data->bl = backlight_device_register("tosa-bl", &client->dev, data, |
| 111 | &bl_ops, &props); |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 112 | if (IS_ERR(data->bl)) { |
| 113 | ret = PTR_ERR(data->bl); |
| 114 | goto err_reg; |
| 115 | } |
| 116 | |
| 117 | data->bl->props.brightness = 69; |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 118 | data->bl->props.power = FB_BLANK_UNBLANK; |
| 119 | |
| 120 | backlight_update_status(data->bl); |
| 121 | |
| 122 | return 0; |
| 123 | |
| 124 | err_reg: |
| 125 | data->bl = NULL; |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 126 | return ret; |
| 127 | } |
| 128 | |
Bill Pemberton | 7e4b9d0 | 2012-11-19 13:26:34 -0500 | [diff] [blame^] | 129 | static int tosa_bl_remove(struct i2c_client *client) |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 130 | { |
| 131 | struct tosa_bl_data *data = i2c_get_clientdata(client); |
| 132 | |
| 133 | backlight_device_unregister(data->bl); |
| 134 | data->bl = NULL; |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 135 | |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | #ifdef CONFIG_PM |
| 140 | static int tosa_bl_suspend(struct i2c_client *client, pm_message_t pm) |
| 141 | { |
| 142 | struct tosa_bl_data *data = i2c_get_clientdata(client); |
| 143 | |
| 144 | tosa_bl_set_backlight(data, 0); |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | static int tosa_bl_resume(struct i2c_client *client) |
| 150 | { |
| 151 | struct tosa_bl_data *data = i2c_get_clientdata(client); |
| 152 | |
| 153 | backlight_update_status(data->bl); |
| 154 | return 0; |
| 155 | } |
| 156 | #else |
| 157 | #define tosa_bl_suspend NULL |
| 158 | #define tosa_bl_resume NULL |
| 159 | #endif |
| 160 | |
| 161 | static const struct i2c_device_id tosa_bl_id[] = { |
| 162 | { "tosa-bl", 0 }, |
| 163 | { }, |
| 164 | }; |
| 165 | |
| 166 | |
| 167 | static struct i2c_driver tosa_bl_driver = { |
| 168 | .driver = { |
| 169 | .name = "tosa-bl", |
| 170 | .owner = THIS_MODULE, |
| 171 | }, |
| 172 | .probe = tosa_bl_probe, |
Bill Pemberton | d1723fa | 2012-11-19 13:21:09 -0500 | [diff] [blame] | 173 | .remove = tosa_bl_remove, |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 174 | .suspend = tosa_bl_suspend, |
| 175 | .resume = tosa_bl_resume, |
| 176 | .id_table = tosa_bl_id, |
| 177 | }; |
| 178 | |
Axel Lin | 81ce686 | 2012-03-23 15:01:59 -0700 | [diff] [blame] | 179 | module_i2c_driver(tosa_bl_driver); |
Dmitry Baryshkov | fbd1b17 | 2008-09-13 20:58:51 +0400 | [diff] [blame] | 180 | |
| 181 | MODULE_AUTHOR("Dmitry Baryshkov"); |
| 182 | MODULE_LICENSE("GPL v2"); |
| 183 | MODULE_DESCRIPTION("LCD/Backlight control for Sharp SL-6000 PDA"); |
| 184 | |