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> |
| 21 | |
| 22 | #include <asm/mach/sharpsl_param.h> |
| 23 | |
| 24 | #include <mach/tosa.h> |
| 25 | |
| 26 | #define COMADJ_DEFAULT 97 |
| 27 | |
| 28 | #define DAC_CH1 0 |
| 29 | #define DAC_CH2 1 |
| 30 | |
| 31 | struct tosa_bl_data { |
| 32 | struct i2c_client *i2c; |
| 33 | struct backlight_device *bl; |
| 34 | |
| 35 | int comadj; |
| 36 | }; |
| 37 | |
| 38 | static void tosa_bl_set_backlight(struct tosa_bl_data *data, int brightness) |
| 39 | { |
| 40 | struct spi_device *spi = data->i2c->dev.platform_data; |
| 41 | |
| 42 | i2c_smbus_write_byte_data(data->i2c, DAC_CH1, data->comadj); |
| 43 | |
| 44 | /* SetBacklightDuty */ |
| 45 | i2c_smbus_write_byte_data(data->i2c, DAC_CH2, (u8)(brightness & 0xff)); |
| 46 | |
| 47 | /* SetBacklightVR */ |
| 48 | gpio_set_value(TOSA_GPIO_BL_C20MA, brightness & 0x100); |
| 49 | |
| 50 | tosa_bl_enable(spi, brightness); |
| 51 | } |
| 52 | |
| 53 | static int tosa_bl_update_status(struct backlight_device *dev) |
| 54 | { |
| 55 | struct backlight_properties *props = &dev->props; |
| 56 | struct tosa_bl_data *data = dev_get_drvdata(&dev->dev); |
| 57 | int power = max(props->power, props->fb_blank); |
| 58 | int brightness = props->brightness; |
| 59 | |
| 60 | if (power) |
| 61 | brightness = 0; |
| 62 | |
| 63 | tosa_bl_set_backlight(data, brightness); |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | static int tosa_bl_get_brightness(struct backlight_device *dev) |
| 69 | { |
| 70 | struct backlight_properties *props = &dev->props; |
| 71 | |
| 72 | return props->brightness; |
| 73 | } |
| 74 | |
| 75 | static struct backlight_ops bl_ops = { |
| 76 | .get_brightness = tosa_bl_get_brightness, |
| 77 | .update_status = tosa_bl_update_status, |
| 78 | }; |
| 79 | |
| 80 | static int __devinit tosa_bl_probe(struct i2c_client *client, |
| 81 | const struct i2c_device_id *id) |
| 82 | { |
| 83 | struct tosa_bl_data *data = kzalloc(sizeof(struct tosa_bl_data), GFP_KERNEL); |
| 84 | int ret = 0; |
| 85 | if (!data) |
| 86 | return -ENOMEM; |
| 87 | |
| 88 | data->comadj = sharpsl_param.comadj == -1 ? COMADJ_DEFAULT : sharpsl_param.comadj; |
| 89 | |
| 90 | ret = gpio_request(TOSA_GPIO_BL_C20MA, "backlight"); |
| 91 | if (ret) { |
| 92 | dev_dbg(&data->bl->dev, "Unable to request gpio!\n"); |
| 93 | goto err_gpio_bl; |
| 94 | } |
| 95 | ret = gpio_direction_output(TOSA_GPIO_BL_C20MA, 0); |
| 96 | if (ret) |
| 97 | goto err_gpio_dir; |
| 98 | |
| 99 | i2c_set_clientdata(client, data); |
| 100 | data->i2c = client; |
| 101 | |
| 102 | data->bl = backlight_device_register("tosa-bl", &client->dev, |
| 103 | data, &bl_ops); |
| 104 | if (IS_ERR(data->bl)) { |
| 105 | ret = PTR_ERR(data->bl); |
| 106 | goto err_reg; |
| 107 | } |
| 108 | |
| 109 | data->bl->props.brightness = 69; |
| 110 | data->bl->props.max_brightness = 512 - 1; |
| 111 | data->bl->props.power = FB_BLANK_UNBLANK; |
| 112 | |
| 113 | backlight_update_status(data->bl); |
| 114 | |
| 115 | return 0; |
| 116 | |
| 117 | err_reg: |
| 118 | data->bl = NULL; |
| 119 | i2c_set_clientdata(client, NULL); |
| 120 | err_gpio_dir: |
| 121 | gpio_free(TOSA_GPIO_BL_C20MA); |
| 122 | err_gpio_bl: |
| 123 | kfree(data); |
| 124 | return ret; |
| 125 | } |
| 126 | |
| 127 | static int __devexit tosa_bl_remove(struct i2c_client *client) |
| 128 | { |
| 129 | struct tosa_bl_data *data = i2c_get_clientdata(client); |
| 130 | |
| 131 | backlight_device_unregister(data->bl); |
| 132 | data->bl = NULL; |
| 133 | i2c_set_clientdata(client, NULL); |
| 134 | |
| 135 | gpio_free(TOSA_GPIO_BL_C20MA); |
| 136 | |
| 137 | kfree(data); |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | #ifdef CONFIG_PM |
| 143 | static int tosa_bl_suspend(struct i2c_client *client, pm_message_t pm) |
| 144 | { |
| 145 | struct tosa_bl_data *data = i2c_get_clientdata(client); |
| 146 | |
| 147 | tosa_bl_set_backlight(data, 0); |
| 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | static int tosa_bl_resume(struct i2c_client *client) |
| 153 | { |
| 154 | struct tosa_bl_data *data = i2c_get_clientdata(client); |
| 155 | |
| 156 | backlight_update_status(data->bl); |
| 157 | return 0; |
| 158 | } |
| 159 | #else |
| 160 | #define tosa_bl_suspend NULL |
| 161 | #define tosa_bl_resume NULL |
| 162 | #endif |
| 163 | |
| 164 | static const struct i2c_device_id tosa_bl_id[] = { |
| 165 | { "tosa-bl", 0 }, |
| 166 | { }, |
| 167 | }; |
| 168 | |
| 169 | |
| 170 | static struct i2c_driver tosa_bl_driver = { |
| 171 | .driver = { |
| 172 | .name = "tosa-bl", |
| 173 | .owner = THIS_MODULE, |
| 174 | }, |
| 175 | .probe = tosa_bl_probe, |
| 176 | .remove = __devexit_p(tosa_bl_remove), |
| 177 | .suspend = tosa_bl_suspend, |
| 178 | .resume = tosa_bl_resume, |
| 179 | .id_table = tosa_bl_id, |
| 180 | }; |
| 181 | |
| 182 | static int __init tosa_bl_init(void) |
| 183 | { |
| 184 | return i2c_add_driver(&tosa_bl_driver); |
| 185 | } |
| 186 | |
| 187 | static void __exit tosa_bl_exit(void) |
| 188 | { |
| 189 | i2c_del_driver(&tosa_bl_driver); |
| 190 | } |
| 191 | |
| 192 | module_init(tosa_bl_init); |
| 193 | module_exit(tosa_bl_exit); |
| 194 | |
| 195 | MODULE_AUTHOR("Dmitry Baryshkov"); |
| 196 | MODULE_LICENSE("GPL v2"); |
| 197 | MODULE_DESCRIPTION("LCD/Backlight control for Sharp SL-6000 PDA"); |
| 198 | |