intel_oaktrail.c 8.7 KB

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