tosa_bl.c 4.3 KB

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