intel_oaktrail.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * intel_oaktrail.c - Intel OakTrail Platform support.
  3. *
  4. * Copyright (C) 2010-2011 Intel Corporation
  5. * Author: Yin Kangkai (kangkai.yin@intel.com)
  6. *
  7. * based on Compal driver, Copyright (C) 2008 Cezary Jackiewicz
  8. * <cezary.jackiewicz (at) gmail.com>, based on MSI driver
  9. * Copyright (C) 2006 Lennart Poettering <mzxreary (at) 0pointer (dot) de>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  24. * 02110-1301, USA.
  25. *
  26. * This driver does below things:
  27. * 1. registers itself in the Linux backlight control in
  28. * /sys/class/backlight/intel_oaktrail/
  29. *
  30. * 2. registers in the rfkill subsystem here: /sys/class/rfkill/rfkillX/
  31. * for these components: wifi, bluetooth, wwan (3g), gps
  32. *
  33. * This driver might work on other products based on Oaktrail. If you
  34. * want to try it you can pass force=1 as argument to the module which
  35. * will force it to load even when the DMI data doesn't identify the
  36. * product as compatible.
  37. */
  38. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  39. #include <linux/module.h>
  40. #include <linux/kernel.h>
  41. #include <linux/init.h>
  42. #include <linux/acpi.h>
  43. #include <linux/fb.h>
  44. #include <linux/mutex.h>
  45. #include <linux/err.h>
  46. #include <linux/i2c.h>
  47. #include <linux/backlight.h>
  48. #include <linux/platform_device.h>
  49. #include <linux/dmi.h>
  50. #include <linux/rfkill.h>
  51. #include <acpi/video.h>
  52. #define DRIVER_NAME "intel_oaktrail"
  53. #define DRIVER_VERSION "0.4ac1"
  54. /*
  55. * This is the devices status address in EC space, and the control bits
  56. * definition:
  57. *
  58. * (1 << 0): Camera enable/disable, RW.
  59. * (1 << 1): Bluetooth enable/disable, RW.
  60. * (1 << 2): GPS enable/disable, RW.
  61. * (1 << 3): WiFi enable/disable, RW.
  62. * (1 << 4): WWAN (3G) enable/disable, RW.
  63. * (1 << 5): Touchscreen enable/disable, Read Only.
  64. */
  65. #define OT_EC_DEVICE_STATE_ADDRESS 0xD6
  66. #define OT_EC_CAMERA_MASK (1 << 0)
  67. #define OT_EC_BT_MASK (1 << 1)
  68. #define OT_EC_GPS_MASK (1 << 2)
  69. #define OT_EC_WIFI_MASK (1 << 3)
  70. #define OT_EC_WWAN_MASK (1 << 4)
  71. #define OT_EC_TS_MASK (1 << 5)
  72. /*
  73. * This is the address in EC space and commands used to control LCD backlight:
  74. *
  75. * Two steps needed to change the LCD backlight:
  76. * 1. write the backlight percentage into OT_EC_BL_BRIGHTNESS_ADDRESS;
  77. * 2. write OT_EC_BL_CONTROL_ON_DATA into OT_EC_BL_CONTROL_ADDRESS.
  78. *
  79. * To read the LCD back light, just read out the value from
  80. * OT_EC_BL_BRIGHTNESS_ADDRESS.
  81. *
  82. * LCD backlight brightness range: 0 - 100 (OT_EC_BL_BRIGHTNESS_MAX)
  83. */
  84. #define OT_EC_BL_BRIGHTNESS_ADDRESS 0x44
  85. #define OT_EC_BL_BRIGHTNESS_MAX 100
  86. #define OT_EC_BL_CONTROL_ADDRESS 0x3A
  87. #define OT_EC_BL_CONTROL_ON_DATA 0x1A
  88. static bool force;
  89. module_param(force, bool, 0);
  90. MODULE_PARM_DESC(force, "Force driver load, ignore DMI data");
  91. static struct platform_device *oaktrail_device;
  92. static struct backlight_device *oaktrail_bl_device;
  93. static struct rfkill *bt_rfkill;
  94. static struct rfkill *gps_rfkill;
  95. static struct rfkill *wifi_rfkill;
  96. static struct rfkill *wwan_rfkill;
  97. /* rfkill */
  98. static int oaktrail_rfkill_set(void *data, bool blocked)
  99. {
  100. u8 value;
  101. u8 result;
  102. unsigned long radio = (unsigned long) data;
  103. ec_read(OT_EC_DEVICE_STATE_ADDRESS, &result);
  104. if (!blocked)
  105. value = (u8) (result | radio);
  106. else
  107. value = (u8) (result & ~radio);
  108. ec_write(OT_EC_DEVICE_STATE_ADDRESS, value);
  109. return 0;
  110. }
  111. static const struct rfkill_ops oaktrail_rfkill_ops = {
  112. .set_block = oaktrail_rfkill_set,
  113. };
  114. static struct rfkill *oaktrail_rfkill_new(char *name, enum rfkill_type type,
  115. unsigned long mask)
  116. {
  117. struct rfkill *rfkill_dev;
  118. u8 value;
  119. int err;
  120. rfkill_dev = rfkill_alloc(name, &oaktrail_device->dev, type,
  121. &oaktrail_rfkill_ops, (void *)mask);
  122. if (!rfkill_dev)
  123. return ERR_PTR(-ENOMEM);
  124. ec_read(OT_EC_DEVICE_STATE_ADDRESS, &value);
  125. rfkill_init_sw_state(rfkill_dev, (value & mask) != 1);
  126. err = rfkill_register(rfkill_dev);
  127. if (err) {
  128. rfkill_destroy(rfkill_dev);
  129. return ERR_PTR(err);
  130. }
  131. return rfkill_dev;
  132. }
  133. static inline void __oaktrail_rfkill_cleanup(struct rfkill *rf)
  134. {
  135. if (rf) {
  136. rfkill_unregister(rf);
  137. rfkill_destroy(rf);
  138. }
  139. }
  140. static void oaktrail_rfkill_cleanup(void)
  141. {
  142. __oaktrail_rfkill_cleanup(wifi_rfkill);
  143. __oaktrail_rfkill_cleanup(bt_rfkill);
  144. __oaktrail_rfkill_cleanup(gps_rfkill);
  145. __oaktrail_rfkill_cleanup(wwan_rfkill);
  146. }
  147. static int oaktrail_rfkill_init(void)
  148. {
  149. int ret;
  150. wifi_rfkill = oaktrail_rfkill_new("oaktrail-wifi",
  151. RFKILL_TYPE_WLAN,
  152. OT_EC_WIFI_MASK);
  153. if (IS_ERR(wifi_rfkill)) {
  154. ret = PTR_ERR(wifi_rfkill);
  155. wifi_rfkill = NULL;
  156. goto cleanup;
  157. }
  158. bt_rfkill = oaktrail_rfkill_new("oaktrail-bluetooth",
  159. RFKILL_TYPE_BLUETOOTH,
  160. OT_EC_BT_MASK);
  161. if (IS_ERR(bt_rfkill)) {
  162. ret = PTR_ERR(bt_rfkill);
  163. bt_rfkill = NULL;
  164. goto cleanup;
  165. }
  166. gps_rfkill = oaktrail_rfkill_new("oaktrail-gps",
  167. RFKILL_TYPE_GPS,
  168. OT_EC_GPS_MASK);
  169. if (IS_ERR(gps_rfkill)) {
  170. ret = PTR_ERR(gps_rfkill);
  171. gps_rfkill = NULL;
  172. goto cleanup;
  173. }
  174. wwan_rfkill = oaktrail_rfkill_new("oaktrail-wwan",
  175. RFKILL_TYPE_WWAN,
  176. OT_EC_WWAN_MASK);
  177. if (IS_ERR(wwan_rfkill)) {
  178. ret = PTR_ERR(wwan_rfkill);
  179. wwan_rfkill = NULL;
  180. goto cleanup;
  181. }
  182. return 0;
  183. cleanup:
  184. oaktrail_rfkill_cleanup();
  185. return ret;
  186. }
  187. /* backlight */
  188. static int get_backlight_brightness(struct backlight_device *b)
  189. {
  190. u8 value;
  191. ec_read(OT_EC_BL_BRIGHTNESS_ADDRESS, &value);
  192. return value;
  193. }
  194. static int set_backlight_brightness(struct backlight_device *b)
  195. {
  196. u8 percent = (u8) b->props.brightness;
  197. if (percent < 0 || percent > OT_EC_BL_BRIGHTNESS_MAX)
  198. return -EINVAL;
  199. ec_write(OT_EC_BL_BRIGHTNESS_ADDRESS, percent);
  200. ec_write(OT_EC_BL_CONTROL_ADDRESS, OT_EC_BL_CONTROL_ON_DATA);
  201. return 0;
  202. }
  203. static const struct backlight_ops oaktrail_bl_ops = {
  204. .get_brightness = get_backlight_brightness,
  205. .update_status = set_backlight_brightness,
  206. };
  207. static int oaktrail_backlight_init(void)
  208. {
  209. struct backlight_device *bd;
  210. struct backlight_properties props;
  211. memset(&props, 0, sizeof(struct backlight_properties));
  212. props.type = BACKLIGHT_PLATFORM;
  213. props.max_brightness = OT_EC_BL_BRIGHTNESS_MAX;
  214. bd = backlight_device_register(DRIVER_NAME,
  215. &oaktrail_device->dev, NULL,
  216. &oaktrail_bl_ops,
  217. &props);
  218. if (IS_ERR(bd)) {
  219. oaktrail_bl_device = NULL;
  220. pr_warning("Unable to register backlight device\n");
  221. return PTR_ERR(bd);
  222. }
  223. oaktrail_bl_device = bd;
  224. bd->props.brightness = get_backlight_brightness(bd);
  225. bd->props.power = FB_BLANK_UNBLANK;
  226. backlight_update_status(bd);
  227. return 0;
  228. }
  229. static void oaktrail_backlight_exit(void)
  230. {
  231. backlight_device_unregister(oaktrail_bl_device);
  232. }
  233. static int oaktrail_probe(struct platform_device *pdev)
  234. {
  235. return 0;
  236. }
  237. static int oaktrail_remove(struct platform_device *pdev)
  238. {
  239. return 0;
  240. }
  241. static struct platform_driver oaktrail_driver = {
  242. .driver = {
  243. .name = DRIVER_NAME,
  244. },
  245. .probe = oaktrail_probe,
  246. .remove = oaktrail_remove,
  247. };
  248. static int dmi_check_cb(const struct dmi_system_id *id)
  249. {
  250. pr_info("Identified model '%s'\n", id->ident);
  251. return 0;
  252. }
  253. static struct dmi_system_id __initdata oaktrail_dmi_table[] = {
  254. {
  255. .ident = "OakTrail platform",
  256. .matches = {
  257. DMI_MATCH(DMI_PRODUCT_NAME, "OakTrail platform"),
  258. },
  259. .callback = dmi_check_cb
  260. },
  261. { }
  262. };
  263. MODULE_DEVICE_TABLE(dmi, oaktrail_dmi_table);
  264. static int __init oaktrail_init(void)
  265. {
  266. int ret;
  267. if (acpi_disabled) {
  268. pr_err("ACPI needs to be enabled for this driver to work!\n");
  269. return -ENODEV;
  270. }
  271. if (!force && !dmi_check_system(oaktrail_dmi_table)) {
  272. pr_err("Platform not recognized (You could try the module's force-parameter)");
  273. return -ENODEV;
  274. }
  275. ret = platform_driver_register(&oaktrail_driver);
  276. if (ret) {
  277. pr_warning("Unable to register platform driver\n");
  278. goto err_driver_reg;
  279. }
  280. oaktrail_device = platform_device_alloc(DRIVER_NAME, -1);
  281. if (!oaktrail_device) {
  282. pr_warning("Unable to allocate platform device\n");
  283. ret = -ENOMEM;
  284. goto err_device_alloc;
  285. }
  286. ret = platform_device_add(oaktrail_device);
  287. if (ret) {
  288. pr_warning("Unable to add platform device\n");
  289. goto err_device_add;
  290. }
  291. if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
  292. ret = oaktrail_backlight_init();
  293. if (ret)
  294. goto err_backlight;
  295. }
  296. ret = oaktrail_rfkill_init();
  297. if (ret) {
  298. pr_warning("Setup rfkill failed\n");
  299. goto err_rfkill;
  300. }
  301. pr_info("Driver "DRIVER_VERSION" successfully loaded\n");
  302. return 0;
  303. err_rfkill:
  304. oaktrail_backlight_exit();
  305. err_backlight:
  306. platform_device_del(oaktrail_device);
  307. err_device_add:
  308. platform_device_put(oaktrail_device);
  309. err_device_alloc:
  310. platform_driver_unregister(&oaktrail_driver);
  311. err_driver_reg:
  312. return ret;
  313. }
  314. static void __exit oaktrail_cleanup(void)
  315. {
  316. oaktrail_backlight_exit();
  317. oaktrail_rfkill_cleanup();
  318. platform_device_unregister(oaktrail_device);
  319. platform_driver_unregister(&oaktrail_driver);
  320. pr_info("Driver unloaded\n");
  321. }
  322. module_init(oaktrail_init);
  323. module_exit(oaktrail_cleanup);
  324. MODULE_AUTHOR("Yin Kangkai (kangkai.yin@intel.com)");
  325. MODULE_DESCRIPTION("Intel Oaktrail Platform ACPI Extras");
  326. MODULE_VERSION(DRIVER_VERSION);
  327. MODULE_LICENSE("GPL");