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 <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 const 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 backlight_properties props;
  64. struct tosa_bl_data *data = kzalloc(sizeof(struct tosa_bl_data), GFP_KERNEL);
  65. int ret = 0;
  66. if (!data)
  67. return -ENOMEM;
  68. data->comadj = sharpsl_param.comadj == -1 ? COMADJ_DEFAULT : sharpsl_param.comadj;
  69. ret = gpio_request(TOSA_GPIO_BL_C20MA, "backlight");
  70. if (ret) {
  71. dev_dbg(&data->bl->dev, "Unable to request gpio!\n");
  72. goto err_gpio_bl;
  73. }
  74. ret = gpio_direction_output(TOSA_GPIO_BL_C20MA, 0);
  75. if (ret)
  76. goto err_gpio_dir;
  77. i2c_set_clientdata(client, data);
  78. data->i2c = client;
  79. memset(&props, 0, sizeof(struct backlight_properties));
  80. props.max_brightness = 512 - 1;
  81. data->bl = backlight_device_register("tosa-bl", &client->dev, data,
  82. &bl_ops, &props);
  83. if (IS_ERR(data->bl)) {
  84. ret = PTR_ERR(data->bl);
  85. goto err_reg;
  86. }
  87. data->bl->props.brightness = 69;
  88. data->bl->props.power = FB_BLANK_UNBLANK;
  89. backlight_update_status(data->bl);
  90. return 0;
  91. err_reg:
  92. data->bl = NULL;
  93. i2c_set_clientdata(client, NULL);
  94. err_gpio_dir:
  95. gpio_free(TOSA_GPIO_BL_C20MA);
  96. err_gpio_bl:
  97. kfree(data);
  98. return ret;
  99. }
  100. static int __devexit tosa_bl_remove(struct i2c_client *client)
  101. {
  102. struct tosa_bl_data *data = i2c_get_clientdata(client);
  103. backlight_device_unregister(data->bl);
  104. data->bl = NULL;
  105. i2c_set_clientdata(client, 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");