tosa_bl.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 = dev_get_platdata(&data->i2c->dev);
  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 = bl_get_data(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 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;
  66. int ret = 0;
  67. data = devm_kzalloc(&client->dev, sizeof(struct tosa_bl_data),
  68. GFP_KERNEL);
  69. if (!data)
  70. return -ENOMEM;
  71. data->comadj = sharpsl_param.comadj == -1 ? COMADJ_DEFAULT : sharpsl_param.comadj;
  72. ret = devm_gpio_request_one(&client->dev, TOSA_GPIO_BL_C20MA,
  73. GPIOF_OUT_INIT_LOW, "backlight");
  74. if (ret) {
  75. dev_dbg(&data->bl->dev, "Unable to request gpio!\n");
  76. return ret;
  77. }
  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 = devm_backlight_device_register(&client->dev, "tosa-bl",
  84. &client->dev, data, &bl_ops,
  85. &props);
  86. if (IS_ERR(data->bl)) {
  87. ret = PTR_ERR(data->bl);
  88. goto err_reg;
  89. }
  90. data->bl->props.brightness = 69;
  91. data->bl->props.power = FB_BLANK_UNBLANK;
  92. backlight_update_status(data->bl);
  93. return 0;
  94. err_reg:
  95. data->bl = NULL;
  96. return ret;
  97. }
  98. static int tosa_bl_remove(struct i2c_client *client)
  99. {
  100. struct tosa_bl_data *data = i2c_get_clientdata(client);
  101. data->bl = NULL;
  102. return 0;
  103. }
  104. #ifdef CONFIG_PM_SLEEP
  105. static int tosa_bl_suspend(struct device *dev)
  106. {
  107. struct tosa_bl_data *data = dev_get_drvdata(dev);
  108. tosa_bl_set_backlight(data, 0);
  109. return 0;
  110. }
  111. static int tosa_bl_resume(struct device *dev)
  112. {
  113. struct tosa_bl_data *data = dev_get_drvdata(dev);
  114. backlight_update_status(data->bl);
  115. return 0;
  116. }
  117. #endif
  118. static SIMPLE_DEV_PM_OPS(tosa_bl_pm_ops, tosa_bl_suspend, tosa_bl_resume);
  119. static const struct i2c_device_id tosa_bl_id[] = {
  120. { "tosa-bl", 0 },
  121. { },
  122. };
  123. MODULE_DEVICE_TABLE(i2c, tosa_bl_id);
  124. static struct i2c_driver tosa_bl_driver = {
  125. .driver = {
  126. .name = "tosa-bl",
  127. .owner = THIS_MODULE,
  128. .pm = &tosa_bl_pm_ops,
  129. },
  130. .probe = tosa_bl_probe,
  131. .remove = tosa_bl_remove,
  132. .id_table = tosa_bl_id,
  133. };
  134. module_i2c_driver(tosa_bl_driver);
  135. MODULE_AUTHOR("Dmitry Baryshkov");
  136. MODULE_LICENSE("GPL v2");
  137. MODULE_DESCRIPTION("LCD/Backlight control for Sharp SL-6000 PDA");