Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  Backlight Driver for the KB3886 Backlight
0004  *
0005  *  Copyright (c) 2007-2008 Claudio Nieder
0006  *
0007  *  Based on corgi_bl.c by Richard Purdie and kb3886 driver by Robert Woerle
0008  */
0009 
0010 #include <linux/module.h>
0011 #include <linux/kernel.h>
0012 #include <linux/init.h>
0013 #include <linux/platform_device.h>
0014 #include <linux/mutex.h>
0015 #include <linux/fb.h>
0016 #include <linux/backlight.h>
0017 #include <linux/delay.h>
0018 #include <linux/dmi.h>
0019 
0020 #define KB3886_PARENT 0x64
0021 #define KB3886_IO 0x60
0022 #define KB3886_ADC_DAC_PWM 0xC4
0023 #define KB3886_PWM0_WRITE 0x81
0024 #define KB3886_PWM0_READ 0x41
0025 
0026 static DEFINE_MUTEX(bl_mutex);
0027 
0028 static void kb3886_bl_set_intensity(int intensity)
0029 {
0030     mutex_lock(&bl_mutex);
0031     intensity = intensity&0xff;
0032     outb(KB3886_ADC_DAC_PWM, KB3886_PARENT);
0033     usleep_range(10000, 11000);
0034     outb(KB3886_PWM0_WRITE, KB3886_IO);
0035     usleep_range(10000, 11000);
0036     outb(intensity, KB3886_IO);
0037     mutex_unlock(&bl_mutex);
0038 }
0039 
0040 struct kb3886bl_machinfo {
0041     int max_intensity;
0042     int default_intensity;
0043     int limit_mask;
0044     void (*set_bl_intensity)(int intensity);
0045 };
0046 
0047 static struct kb3886bl_machinfo kb3886_bl_machinfo = {
0048     .max_intensity = 0xff,
0049     .default_intensity = 0xa0,
0050     .limit_mask = 0x7f,
0051     .set_bl_intensity = kb3886_bl_set_intensity,
0052 };
0053 
0054 static struct platform_device kb3886bl_device = {
0055     .name       = "kb3886-bl",
0056     .dev        = {
0057         .platform_data  = &kb3886_bl_machinfo,
0058     },
0059     .id     = -1,
0060 };
0061 
0062 static struct platform_device *devices[] __initdata = {
0063     &kb3886bl_device,
0064 };
0065 
0066 /*
0067  * Back to driver
0068  */
0069 
0070 static int kb3886bl_intensity;
0071 static struct backlight_device *kb3886_backlight_device;
0072 static struct kb3886bl_machinfo *bl_machinfo;
0073 
0074 static unsigned long kb3886bl_flags;
0075 #define KB3886BL_SUSPENDED     0x01
0076 
0077 static const struct dmi_system_id kb3886bl_device_table[] __initconst = {
0078     {
0079         .ident = "Sahara Touch-iT",
0080         .matches = {
0081             DMI_MATCH(DMI_SYS_VENDOR, "SDV"),
0082             DMI_MATCH(DMI_PRODUCT_NAME, "iTouch T201"),
0083         },
0084     },
0085     { }
0086 };
0087 
0088 static int kb3886bl_send_intensity(struct backlight_device *bd)
0089 {
0090     int intensity = backlight_get_brightness(bd);
0091 
0092     if (kb3886bl_flags & KB3886BL_SUSPENDED)
0093         intensity = 0;
0094 
0095     bl_machinfo->set_bl_intensity(intensity);
0096 
0097     kb3886bl_intensity = intensity;
0098     return 0;
0099 }
0100 
0101 #ifdef CONFIG_PM_SLEEP
0102 static int kb3886bl_suspend(struct device *dev)
0103 {
0104     struct backlight_device *bd = dev_get_drvdata(dev);
0105 
0106     kb3886bl_flags |= KB3886BL_SUSPENDED;
0107     backlight_update_status(bd);
0108     return 0;
0109 }
0110 
0111 static int kb3886bl_resume(struct device *dev)
0112 {
0113     struct backlight_device *bd = dev_get_drvdata(dev);
0114 
0115     kb3886bl_flags &= ~KB3886BL_SUSPENDED;
0116     backlight_update_status(bd);
0117     return 0;
0118 }
0119 #endif
0120 
0121 static SIMPLE_DEV_PM_OPS(kb3886bl_pm_ops, kb3886bl_suspend, kb3886bl_resume);
0122 
0123 static int kb3886bl_get_intensity(struct backlight_device *bd)
0124 {
0125     return kb3886bl_intensity;
0126 }
0127 
0128 static const struct backlight_ops kb3886bl_ops = {
0129     .get_brightness = kb3886bl_get_intensity,
0130     .update_status  = kb3886bl_send_intensity,
0131 };
0132 
0133 static int kb3886bl_probe(struct platform_device *pdev)
0134 {
0135     struct backlight_properties props;
0136     struct kb3886bl_machinfo *machinfo = dev_get_platdata(&pdev->dev);
0137 
0138     bl_machinfo = machinfo;
0139     if (!machinfo->limit_mask)
0140         machinfo->limit_mask = -1;
0141 
0142     memset(&props, 0, sizeof(struct backlight_properties));
0143     props.type = BACKLIGHT_RAW;
0144     props.max_brightness = machinfo->max_intensity;
0145     kb3886_backlight_device = devm_backlight_device_register(&pdev->dev,
0146                             "kb3886-bl", &pdev->dev,
0147                             NULL, &kb3886bl_ops,
0148                             &props);
0149     if (IS_ERR(kb3886_backlight_device))
0150         return PTR_ERR(kb3886_backlight_device);
0151 
0152     platform_set_drvdata(pdev, kb3886_backlight_device);
0153 
0154     kb3886_backlight_device->props.power = FB_BLANK_UNBLANK;
0155     kb3886_backlight_device->props.brightness = machinfo->default_intensity;
0156     backlight_update_status(kb3886_backlight_device);
0157 
0158     return 0;
0159 }
0160 
0161 static struct platform_driver kb3886bl_driver = {
0162     .probe      = kb3886bl_probe,
0163     .driver     = {
0164         .name   = "kb3886-bl",
0165         .pm = &kb3886bl_pm_ops,
0166     },
0167 };
0168 
0169 static int __init kb3886_init(void)
0170 {
0171     if (!dmi_check_system(kb3886bl_device_table))
0172         return -ENODEV;
0173 
0174     platform_add_devices(devices, ARRAY_SIZE(devices));
0175     return platform_driver_register(&kb3886bl_driver);
0176 }
0177 
0178 static void __exit kb3886_exit(void)
0179 {
0180     platform_driver_unregister(&kb3886bl_driver);
0181 }
0182 
0183 module_init(kb3886_init);
0184 module_exit(kb3886_exit);
0185 
0186 MODULE_AUTHOR("Claudio Nieder <private@claudio.ch>");
0187 MODULE_DESCRIPTION("Tabletkiosk Sahara Touch-iT Backlight Driver");
0188 MODULE_LICENSE("GPL");
0189 MODULE_ALIAS("dmi:*:svnSDV:pniTouchT201:*");