1 /* 2 * Backlight driver for Pandora handheld. 3 * Pandora uses TWL4030 PWM0 -> TPS61161 combo for control backlight. 4 * Based on pwm_bl.c 5 * 6 * Copyright 2009,2012 Gražvydas Ignotas <notasas@gmail.com> 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License version 2 as 10 * published by the Free Software Foundation. 11 */ 12 13 #include <linux/module.h> 14 #include <linux/kernel.h> 15 #include <linux/platform_device.h> 16 #include <linux/delay.h> 17 #include <linux/fb.h> 18 #include <linux/backlight.h> 19 #include <linux/mfd/twl.h> 20 #include <linux/err.h> 21 22 #define TWL_PWM0_ON 0x00 23 #define TWL_PWM0_OFF 0x01 24 25 #define TWL_INTBR_GPBR1 0x0c 26 #define TWL_INTBR_PMBR1 0x0d 27 28 #define TWL_PMBR1_PWM0_MUXMASK 0x0c 29 #define TWL_PMBR1_PWM0 0x04 30 #define PWM0_CLK_ENABLE BIT(0) 31 #define PWM0_ENABLE BIT(2) 32 33 /* range accepted by hardware */ 34 #define MIN_VALUE 9 35 #define MAX_VALUE 63 36 #define MAX_USER_VALUE (MAX_VALUE - MIN_VALUE) 37 38 struct pandora_private { 39 unsigned old_state; 40 #define PANDORABL_WAS_OFF 1 41 }; 42 43 static int pandora_backlight_update_status(struct backlight_device *bl) 44 { 45 int brightness = bl->props.brightness; 46 struct pandora_private *priv = bl_get_data(bl); 47 u8 r; 48 49 if (bl->props.power != FB_BLANK_UNBLANK) 50 brightness = 0; 51 if (bl->props.state & BL_CORE_FBBLANK) 52 brightness = 0; 53 if (bl->props.state & BL_CORE_SUSPENDED) 54 brightness = 0; 55 56 if ((unsigned int)brightness > MAX_USER_VALUE) 57 brightness = MAX_USER_VALUE; 58 59 if (brightness == 0) { 60 if (priv->old_state == PANDORABL_WAS_OFF) 61 goto done; 62 63 /* first disable PWM0 output, then clock */ 64 twl_i2c_read_u8(TWL4030_MODULE_INTBR, &r, TWL_INTBR_GPBR1); 65 r &= ~PWM0_ENABLE; 66 twl_i2c_write_u8(TWL4030_MODULE_INTBR, r, TWL_INTBR_GPBR1); 67 r &= ~PWM0_CLK_ENABLE; 68 twl_i2c_write_u8(TWL4030_MODULE_INTBR, r, TWL_INTBR_GPBR1); 69 70 goto done; 71 } 72 73 if (priv->old_state == PANDORABL_WAS_OFF) { 74 /* 75 * set PWM duty cycle to max. TPS61161 seems to use this 76 * to calibrate it's PWM sensitivity when it starts. 77 */ 78 twl_i2c_write_u8(TWL_MODULE_PWM, MAX_VALUE, TWL_PWM0_OFF); 79 80 /* first enable clock, then PWM0 out */ 81 twl_i2c_read_u8(TWL4030_MODULE_INTBR, &r, TWL_INTBR_GPBR1); 82 r &= ~PWM0_ENABLE; 83 r |= PWM0_CLK_ENABLE; 84 twl_i2c_write_u8(TWL4030_MODULE_INTBR, r, TWL_INTBR_GPBR1); 85 r |= PWM0_ENABLE; 86 twl_i2c_write_u8(TWL4030_MODULE_INTBR, r, TWL_INTBR_GPBR1); 87 88 /* 89 * TI made it very easy to enable digital control, so easy that 90 * it often triggers unintentionally and disabes PWM control, 91 * so wait until 1 wire mode detection window ends. 92 */ 93 usleep_range(2000, 10000); 94 } 95 96 twl_i2c_write_u8(TWL_MODULE_PWM, MIN_VALUE + brightness, TWL_PWM0_OFF); 97 98 done: 99 if (brightness != 0) 100 priv->old_state = 0; 101 else 102 priv->old_state = PANDORABL_WAS_OFF; 103 104 return 0; 105 } 106 107 static const struct backlight_ops pandora_backlight_ops = { 108 .options = BL_CORE_SUSPENDRESUME, 109 .update_status = pandora_backlight_update_status, 110 }; 111 112 static int pandora_backlight_probe(struct platform_device *pdev) 113 { 114 struct backlight_properties props; 115 struct backlight_device *bl; 116 struct pandora_private *priv; 117 u8 r; 118 119 priv = devm_kmalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); 120 if (!priv) { 121 dev_err(&pdev->dev, "failed to allocate driver private data\n"); 122 return -ENOMEM; 123 } 124 125 memset(&props, 0, sizeof(props)); 126 props.max_brightness = MAX_USER_VALUE; 127 props.type = BACKLIGHT_RAW; 128 bl = devm_backlight_device_register(&pdev->dev, pdev->name, &pdev->dev, 129 priv, &pandora_backlight_ops, &props); 130 if (IS_ERR(bl)) { 131 dev_err(&pdev->dev, "failed to register backlight\n"); 132 return PTR_ERR(bl); 133 } 134 135 platform_set_drvdata(pdev, bl); 136 137 /* 64 cycle period, ON position 0 */ 138 twl_i2c_write_u8(TWL_MODULE_PWM, 0x80, TWL_PWM0_ON); 139 140 priv->old_state = PANDORABL_WAS_OFF; 141 bl->props.brightness = MAX_USER_VALUE; 142 backlight_update_status(bl); 143 144 /* enable PWM function in pin mux */ 145 twl_i2c_read_u8(TWL4030_MODULE_INTBR, &r, TWL_INTBR_PMBR1); 146 r &= ~TWL_PMBR1_PWM0_MUXMASK; 147 r |= TWL_PMBR1_PWM0; 148 twl_i2c_write_u8(TWL4030_MODULE_INTBR, r, TWL_INTBR_PMBR1); 149 150 return 0; 151 } 152 153 static struct platform_driver pandora_backlight_driver = { 154 .driver = { 155 .name = "pandora-backlight", 156 }, 157 .probe = pandora_backlight_probe, 158 }; 159 160 module_platform_driver(pandora_backlight_driver); 161 162 MODULE_AUTHOR("Gražvydas Ignotas <notasas@gmail.com>"); 163 MODULE_DESCRIPTION("Pandora Backlight Driver"); 164 MODULE_LICENSE("GPL"); 165 MODULE_ALIAS("platform:pandora-backlight"); 166