leds-ss4200.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /*
  2. * SS4200-E Hardware API
  3. * Copyright (c) 2009, Intel Corporation.
  4. * Copyright IBM Corporation, 2009
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Author: Dave Hansen <dave@sr71.net>
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/dmi.h>
  23. #include <linux/init.h>
  24. #include <linux/ioport.h>
  25. #include <linux/kernel.h>
  26. #include <linux/leds.h>
  27. #include <linux/module.h>
  28. #include <linux/pci.h>
  29. #include <linux/types.h>
  30. #include <linux/uaccess.h>
  31. MODULE_AUTHOR("Rodney Girod <rgirod@confocus.com>, Dave Hansen <dave@sr71.net>");
  32. MODULE_DESCRIPTION("Intel NAS/Home Server ICH7 GPIO Driver");
  33. MODULE_LICENSE("GPL");
  34. /*
  35. * ICH7 LPC/GPIO PCI Config register offsets
  36. */
  37. #define PMBASE 0x040
  38. #define GPIO_BASE 0x048
  39. #define GPIO_CTRL 0x04c
  40. #define GPIO_EN 0x010
  41. /*
  42. * The ICH7 GPIO register block is 64 bytes in size.
  43. */
  44. #define ICH7_GPIO_SIZE 64
  45. /*
  46. * Define register offsets within the ICH7 register block.
  47. */
  48. #define GPIO_USE_SEL 0x000
  49. #define GP_IO_SEL 0x004
  50. #define GP_LVL 0x00c
  51. #define GPO_BLINK 0x018
  52. #define GPI_INV 0x030
  53. #define GPIO_USE_SEL2 0x034
  54. #define GP_IO_SEL2 0x038
  55. #define GP_LVL2 0x03c
  56. /*
  57. * PCI ID of the Intel ICH7 LPC Device within which the GPIO block lives.
  58. */
  59. static const struct pci_device_id ich7_lpc_pci_id[] =
  60. {
  61. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7_0) },
  62. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7_1) },
  63. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH7_30) },
  64. { } /* NULL entry */
  65. };
  66. MODULE_DEVICE_TABLE(pci, ich7_lpc_pci_id);
  67. static int __init ss4200_led_dmi_callback(const struct dmi_system_id *id)
  68. {
  69. pr_info("detected '%s'\n", id->ident);
  70. return 1;
  71. }
  72. static unsigned int __initdata nodetect;
  73. module_param_named(nodetect, nodetect, bool, 0);
  74. MODULE_PARM_DESC(nodetect, "Skip DMI-based hardware detection");
  75. /*
  76. * struct nas_led_whitelist - List of known good models
  77. *
  78. * Contains the known good models this driver is compatible with.
  79. * When adding a new model try to be as strict as possible. This
  80. * makes it possible to keep the false positives (the model is
  81. * detected as working, but in reality it is not) as low as
  82. * possible.
  83. */
  84. static struct dmi_system_id __initdata nas_led_whitelist[] = {
  85. {
  86. .callback = ss4200_led_dmi_callback,
  87. .ident = "Intel SS4200-E",
  88. .matches = {
  89. DMI_MATCH(DMI_SYS_VENDOR, "Intel"),
  90. DMI_MATCH(DMI_PRODUCT_NAME, "SS4200-E"),
  91. DMI_MATCH(DMI_PRODUCT_VERSION, "1.00.00")
  92. }
  93. },
  94. {}
  95. };
  96. /*
  97. * Base I/O address assigned to the Power Management register block
  98. */
  99. static u32 g_pm_io_base;
  100. /*
  101. * Base I/O address assigned to the ICH7 GPIO register block
  102. */
  103. static u32 nas_gpio_io_base;
  104. /*
  105. * When we successfully register a region, we are returned a resource.
  106. * We use these to identify which regions we need to release on our way
  107. * back out.
  108. */
  109. static struct resource *gp_gpio_resource;
  110. struct nasgpio_led {
  111. char *name;
  112. u32 gpio_bit;
  113. struct led_classdev led_cdev;
  114. };
  115. /*
  116. * gpio_bit(s) are the ICH7 GPIO bit assignments
  117. */
  118. static struct nasgpio_led nasgpio_leds[] = {
  119. { .name = "hdd1:blue:sata", .gpio_bit = 0 },
  120. { .name = "hdd1:amber:sata", .gpio_bit = 1 },
  121. { .name = "hdd2:blue:sata", .gpio_bit = 2 },
  122. { .name = "hdd2:amber:sata", .gpio_bit = 3 },
  123. { .name = "hdd3:blue:sata", .gpio_bit = 4 },
  124. { .name = "hdd3:amber:sata", .gpio_bit = 5 },
  125. { .name = "hdd4:blue:sata", .gpio_bit = 6 },
  126. { .name = "hdd4:amber:sata", .gpio_bit = 7 },
  127. { .name = "power:blue:power", .gpio_bit = 27},
  128. { .name = "power:amber:power", .gpio_bit = 28},
  129. };
  130. #define NAS_RECOVERY 0x00000400 /* GPIO10 */
  131. static struct nasgpio_led *
  132. led_classdev_to_nasgpio_led(struct led_classdev *led_cdev)
  133. {
  134. return container_of(led_cdev, struct nasgpio_led, led_cdev);
  135. }
  136. static struct nasgpio_led *get_led_named(char *name)
  137. {
  138. int i;
  139. for (i = 0; i < ARRAY_SIZE(nasgpio_leds); i++) {
  140. if (strcmp(nasgpio_leds[i].name, name))
  141. continue;
  142. return &nasgpio_leds[i];
  143. }
  144. return NULL;
  145. }
  146. /*
  147. * This protects access to the gpio ports.
  148. */
  149. static DEFINE_SPINLOCK(nasgpio_gpio_lock);
  150. /*
  151. * There are two gpio ports, one for blinking and the other
  152. * for power. @port tells us if we're doing blinking or
  153. * power control.
  154. *
  155. * Caller must hold nasgpio_gpio_lock
  156. */
  157. static void __nasgpio_led_set_attr(struct led_classdev *led_cdev,
  158. u32 port, u32 value)
  159. {
  160. struct nasgpio_led *led = led_classdev_to_nasgpio_led(led_cdev);
  161. u32 gpio_out;
  162. gpio_out = inl(nas_gpio_io_base + port);
  163. if (value)
  164. gpio_out |= (1<<led->gpio_bit);
  165. else
  166. gpio_out &= ~(1<<led->gpio_bit);
  167. outl(gpio_out, nas_gpio_io_base + port);
  168. }
  169. static void nasgpio_led_set_attr(struct led_classdev *led_cdev,
  170. u32 port, u32 value)
  171. {
  172. spin_lock(&nasgpio_gpio_lock);
  173. __nasgpio_led_set_attr(led_cdev, port, value);
  174. spin_unlock(&nasgpio_gpio_lock);
  175. }
  176. u32 nasgpio_led_get_attr(struct led_classdev *led_cdev, u32 port)
  177. {
  178. struct nasgpio_led *led = led_classdev_to_nasgpio_led(led_cdev);
  179. u32 gpio_in;
  180. spin_lock(&nasgpio_gpio_lock);
  181. gpio_in = inl(nas_gpio_io_base + port);
  182. spin_unlock(&nasgpio_gpio_lock);
  183. if (gpio_in & (1<<led->gpio_bit))
  184. return 1;
  185. return 0;
  186. }
  187. /*
  188. * There is actual brightness control in the hardware,
  189. * but it is via smbus commands and not implemented
  190. * in this driver.
  191. */
  192. static void nasgpio_led_set_brightness(struct led_classdev *led_cdev,
  193. enum led_brightness brightness)
  194. {
  195. u32 setting = 0;
  196. if (brightness >= LED_HALF)
  197. setting = 1;
  198. /*
  199. * Hold the lock across both operations. This ensures
  200. * consistency so that both the "turn off blinking"
  201. * and "turn light off" operations complete as a set.
  202. */
  203. spin_lock(&nasgpio_gpio_lock);
  204. /*
  205. * LED class documentation asks that past blink state
  206. * be disabled when brightness is turned to zero.
  207. */
  208. if (brightness == 0)
  209. __nasgpio_led_set_attr(led_cdev, GPO_BLINK, 0);
  210. __nasgpio_led_set_attr(led_cdev, GP_LVL, setting);
  211. spin_unlock(&nasgpio_gpio_lock);
  212. }
  213. static int nasgpio_led_set_blink(struct led_classdev *led_cdev,
  214. unsigned long *delay_on,
  215. unsigned long *delay_off)
  216. {
  217. u32 setting = 1;
  218. if (!(*delay_on == 0 && *delay_off == 0) &&
  219. !(*delay_on == 500 && *delay_off == 500))
  220. return -EINVAL;
  221. /*
  222. * These are very approximate.
  223. */
  224. *delay_on = 500;
  225. *delay_off = 500;
  226. nasgpio_led_set_attr(led_cdev, GPO_BLINK, setting);
  227. return 0;
  228. }
  229. /*
  230. * Initialize the ICH7 GPIO registers for NAS usage. The BIOS should have
  231. * already taken care of this, but we will do so in a non destructive manner
  232. * so that we have what we need whether the BIOS did it or not.
  233. */
  234. static int __devinit ich7_gpio_init(struct device *dev)
  235. {
  236. int i;
  237. u32 config_data = 0;
  238. u32 all_nas_led = 0;
  239. for (i = 0; i < ARRAY_SIZE(nasgpio_leds); i++)
  240. all_nas_led |= (1<<nasgpio_leds[i].gpio_bit);
  241. spin_lock(&nasgpio_gpio_lock);
  242. /*
  243. * We need to enable all of the GPIO lines used by the NAS box,
  244. * so we will read the current Use Selection and add our usage
  245. * to it. This should be benign with regard to the original
  246. * BIOS configuration.
  247. */
  248. config_data = inl(nas_gpio_io_base + GPIO_USE_SEL);
  249. dev_dbg(dev, ": Data read from GPIO_USE_SEL = 0x%08x\n", config_data);
  250. config_data |= all_nas_led + NAS_RECOVERY;
  251. outl(config_data, nas_gpio_io_base + GPIO_USE_SEL);
  252. config_data = inl(nas_gpio_io_base + GPIO_USE_SEL);
  253. dev_dbg(dev, ": GPIO_USE_SEL = 0x%08x\n\n", config_data);
  254. /*
  255. * The LED GPIO outputs need to be configured for output, so we
  256. * will ensure that all LED lines are cleared for output and the
  257. * RECOVERY line ready for input. This too should be benign with
  258. * regard to BIOS configuration.
  259. */
  260. config_data = inl(nas_gpio_io_base + GP_IO_SEL);
  261. dev_dbg(dev, ": Data read from GP_IO_SEL = 0x%08x\n",
  262. config_data);
  263. config_data &= ~all_nas_led;
  264. config_data |= NAS_RECOVERY;
  265. outl(config_data, nas_gpio_io_base + GP_IO_SEL);
  266. config_data = inl(nas_gpio_io_base + GP_IO_SEL);
  267. dev_dbg(dev, ": GP_IO_SEL = 0x%08x\n", config_data);
  268. /*
  269. * In our final system, the BIOS will initialize the state of all
  270. * of the LEDs. For now, we turn them all off (or Low).
  271. */
  272. config_data = inl(nas_gpio_io_base + GP_LVL);
  273. dev_dbg(dev, ": Data read from GP_LVL = 0x%08x\n", config_data);
  274. /*
  275. * In our final system, the BIOS will initialize the blink state of all
  276. * of the LEDs. For now, we turn blink off for all of them.
  277. */
  278. config_data = inl(nas_gpio_io_base + GPO_BLINK);
  279. dev_dbg(dev, ": Data read from GPO_BLINK = 0x%08x\n", config_data);
  280. /*
  281. * At this moment, I am unsure if anything needs to happen with GPI_INV
  282. */
  283. config_data = inl(nas_gpio_io_base + GPI_INV);
  284. dev_dbg(dev, ": Data read from GPI_INV = 0x%08x\n", config_data);
  285. spin_unlock(&nasgpio_gpio_lock);
  286. return 0;
  287. }
  288. static void ich7_lpc_cleanup(struct device *dev)
  289. {
  290. /*
  291. * If we were given exclusive use of the GPIO
  292. * I/O Address range, we must return it.
  293. */
  294. if (gp_gpio_resource) {
  295. dev_dbg(dev, ": Releasing GPIO I/O addresses\n");
  296. release_region(nas_gpio_io_base, ICH7_GPIO_SIZE);
  297. gp_gpio_resource = NULL;
  298. }
  299. }
  300. /*
  301. * The OS has determined that the LPC of the Intel ICH7 Southbridge is present
  302. * so we can retrive the required operational information and prepare the GPIO.
  303. */
  304. static struct pci_dev *nas_gpio_pci_dev;
  305. static int __devinit ich7_lpc_probe(struct pci_dev *dev,
  306. const struct pci_device_id *id)
  307. {
  308. int status;
  309. u32 gc = 0;
  310. status = pci_enable_device(dev);
  311. if (status) {
  312. dev_err(&dev->dev, "pci_enable_device failed\n");
  313. return -EIO;
  314. }
  315. nas_gpio_pci_dev = dev;
  316. status = pci_read_config_dword(dev, PMBASE, &g_pm_io_base);
  317. if (status)
  318. goto out;
  319. g_pm_io_base &= 0x00000ff80;
  320. status = pci_read_config_dword(dev, GPIO_CTRL, &gc);
  321. if (!(GPIO_EN & gc)) {
  322. status = -EEXIST;
  323. dev_info(&dev->dev,
  324. "ERROR: The LPC GPIO Block has not been enabled.\n");
  325. goto out;
  326. }
  327. status = pci_read_config_dword(dev, GPIO_BASE, &nas_gpio_io_base);
  328. if (0 > status) {
  329. dev_info(&dev->dev, "Unable to read GPIOBASE.\n");
  330. goto out;
  331. }
  332. dev_dbg(&dev->dev, ": GPIOBASE = 0x%08x\n", nas_gpio_io_base);
  333. nas_gpio_io_base &= 0x00000ffc0;
  334. /*
  335. * Insure that we have exclusive access to the GPIO I/O address range.
  336. */
  337. gp_gpio_resource = request_region(nas_gpio_io_base, ICH7_GPIO_SIZE,
  338. KBUILD_MODNAME);
  339. if (NULL == gp_gpio_resource) {
  340. dev_info(&dev->dev,
  341. "ERROR Unable to register GPIO I/O addresses.\n");
  342. status = -1;
  343. goto out;
  344. }
  345. /*
  346. * Initialize the GPIO for NAS/Home Server Use
  347. */
  348. ich7_gpio_init(&dev->dev);
  349. out:
  350. if (status) {
  351. ich7_lpc_cleanup(&dev->dev);
  352. pci_disable_device(dev);
  353. }
  354. return status;
  355. }
  356. static void ich7_lpc_remove(struct pci_dev *dev)
  357. {
  358. ich7_lpc_cleanup(&dev->dev);
  359. pci_disable_device(dev);
  360. }
  361. /*
  362. * pci_driver structure passed to the PCI modules
  363. */
  364. static struct pci_driver nas_gpio_pci_driver = {
  365. .name = KBUILD_MODNAME,
  366. .id_table = ich7_lpc_pci_id,
  367. .probe = ich7_lpc_probe,
  368. .remove = ich7_lpc_remove,
  369. };
  370. static struct led_classdev *get_classdev_for_led_nr(int nr)
  371. {
  372. struct nasgpio_led *nas_led = &nasgpio_leds[nr];
  373. struct led_classdev *led = &nas_led->led_cdev;
  374. return led;
  375. }
  376. static void set_power_light_amber_noblink(void)
  377. {
  378. struct nasgpio_led *amber = get_led_named("power:amber:power");
  379. struct nasgpio_led *blue = get_led_named("power:blue:power");
  380. if (!amber || !blue)
  381. return;
  382. /*
  383. * LED_OFF implies disabling future blinking
  384. */
  385. pr_debug("setting blue off and amber on\n");
  386. nasgpio_led_set_brightness(&blue->led_cdev, LED_OFF);
  387. nasgpio_led_set_brightness(&amber->led_cdev, LED_FULL);
  388. }
  389. static ssize_t nas_led_blink_show(struct device *dev,
  390. struct device_attribute *attr, char *buf)
  391. {
  392. struct led_classdev *led = dev_get_drvdata(dev);
  393. int blinking = 0;
  394. if (nasgpio_led_get_attr(led, GPO_BLINK))
  395. blinking = 1;
  396. return sprintf(buf, "%u\n", blinking);
  397. }
  398. static ssize_t nas_led_blink_store(struct device *dev,
  399. struct device_attribute *attr,
  400. const char *buf, size_t size)
  401. {
  402. int ret;
  403. struct led_classdev *led = dev_get_drvdata(dev);
  404. unsigned long blink_state;
  405. ret = strict_strtoul(buf, 10, &blink_state);
  406. if (ret)
  407. return ret;
  408. nasgpio_led_set_attr(led, GPO_BLINK, blink_state);
  409. return size;
  410. }
  411. static DEVICE_ATTR(blink, 0644, nas_led_blink_show, nas_led_blink_store);
  412. static int register_nasgpio_led(int led_nr)
  413. {
  414. int ret;
  415. struct nasgpio_led *nas_led = &nasgpio_leds[led_nr];
  416. struct led_classdev *led = get_classdev_for_led_nr(led_nr);
  417. led->name = nas_led->name;
  418. led->brightness = LED_OFF;
  419. if (nasgpio_led_get_attr(led, GP_LVL))
  420. led->brightness = LED_FULL;
  421. led->brightness_set = nasgpio_led_set_brightness;
  422. led->blink_set = nasgpio_led_set_blink;
  423. ret = led_classdev_register(&nas_gpio_pci_dev->dev, led);
  424. if (ret)
  425. return ret;
  426. ret = device_create_file(led->dev, &dev_attr_blink);
  427. if (ret)
  428. led_classdev_unregister(led);
  429. return ret;
  430. }
  431. static void unregister_nasgpio_led(int led_nr)
  432. {
  433. struct led_classdev *led = get_classdev_for_led_nr(led_nr);
  434. led_classdev_unregister(led);
  435. device_remove_file(led->dev, &dev_attr_blink);
  436. }
  437. /*
  438. * module load/initialization
  439. */
  440. static int __init nas_gpio_init(void)
  441. {
  442. int i;
  443. int ret = 0;
  444. int nr_devices = 0;
  445. nr_devices = dmi_check_system(nas_led_whitelist);
  446. if (nodetect) {
  447. pr_info("skipping hardware autodetection\n");
  448. pr_info("Please send 'dmidecode' output to dave@sr71.net\n");
  449. nr_devices++;
  450. }
  451. if (nr_devices <= 0) {
  452. pr_info("no LED devices found\n");
  453. return -ENODEV;
  454. }
  455. pr_info("registering PCI driver\n");
  456. ret = pci_register_driver(&nas_gpio_pci_driver);
  457. if (ret)
  458. return ret;
  459. for (i = 0; i < ARRAY_SIZE(nasgpio_leds); i++) {
  460. ret = register_nasgpio_led(i);
  461. if (ret)
  462. goto out_err;
  463. }
  464. /*
  465. * When the system powers on, the BIOS leaves the power
  466. * light blue and blinking. This will turn it solid
  467. * amber once the driver is loaded.
  468. */
  469. set_power_light_amber_noblink();
  470. return 0;
  471. out_err:
  472. for (i--; i >= 0; i--)
  473. unregister_nasgpio_led(i);
  474. pci_unregister_driver(&nas_gpio_pci_driver);
  475. return ret;
  476. }
  477. /*
  478. * module unload
  479. */
  480. static void __exit nas_gpio_exit(void)
  481. {
  482. int i;
  483. pr_info("Unregistering driver\n");
  484. for (i = 0; i < ARRAY_SIZE(nasgpio_leds); i++)
  485. unregister_nasgpio_led(i);
  486. pci_unregister_driver(&nas_gpio_pci_driver);
  487. }
  488. module_init(nas_gpio_init);
  489. module_exit(nas_gpio_exit);