tosa_bl.c 4.2 KB

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