ics932s401.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /*
  2. * A driver for the Integrated Circuits ICS932S401
  3. * Copyright (C) 2008 IBM
  4. *
  5. * Author: Darrick J. Wong <djwong@us.ibm.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/module.h>
  22. #include <linux/jiffies.h>
  23. #include <linux/i2c.h>
  24. #include <linux/err.h>
  25. #include <linux/mutex.h>
  26. #include <linux/delay.h>
  27. #include <linux/log2.h>
  28. #include <linux/slab.h>
  29. /* Addresses to scan */
  30. static const unsigned short normal_i2c[] = { 0x69, I2C_CLIENT_END };
  31. /* ICS932S401 registers */
  32. #define ICS932S401_REG_CFG2 0x01
  33. #define ICS932S401_CFG1_SPREAD 0x01
  34. #define ICS932S401_REG_CFG7 0x06
  35. #define ICS932S401_FS_MASK 0x07
  36. #define ICS932S401_REG_VENDOR_REV 0x07
  37. #define ICS932S401_VENDOR 1
  38. #define ICS932S401_VENDOR_MASK 0x0F
  39. #define ICS932S401_REV 4
  40. #define ICS932S401_REV_SHIFT 4
  41. #define ICS932S401_REG_DEVICE 0x09
  42. #define ICS932S401_DEVICE 11
  43. #define ICS932S401_REG_CTRL 0x0A
  44. #define ICS932S401_MN_ENABLED 0x80
  45. #define ICS932S401_CPU_ALT 0x04
  46. #define ICS932S401_SRC_ALT 0x08
  47. #define ICS932S401_REG_CPU_M_CTRL 0x0B
  48. #define ICS932S401_M_MASK 0x3F
  49. #define ICS932S401_REG_CPU_N_CTRL 0x0C
  50. #define ICS932S401_REG_CPU_SPREAD1 0x0D
  51. #define ICS932S401_REG_CPU_SPREAD2 0x0E
  52. #define ICS932S401_SPREAD_MASK 0x7FFF
  53. #define ICS932S401_REG_SRC_M_CTRL 0x0F
  54. #define ICS932S401_REG_SRC_N_CTRL 0x10
  55. #define ICS932S401_REG_SRC_SPREAD1 0x11
  56. #define ICS932S401_REG_SRC_SPREAD2 0x12
  57. #define ICS932S401_REG_CPU_DIVISOR 0x13
  58. #define ICS932S401_CPU_DIVISOR_SHIFT 4
  59. #define ICS932S401_REG_PCISRC_DIVISOR 0x14
  60. #define ICS932S401_SRC_DIVISOR_MASK 0x0F
  61. #define ICS932S401_PCI_DIVISOR_SHIFT 4
  62. /* Base clock is 14.318MHz */
  63. #define BASE_CLOCK 14318
  64. #define NUM_REGS 21
  65. #define NUM_MIRRORED_REGS 15
  66. static int regs_to_copy[NUM_MIRRORED_REGS] = {
  67. ICS932S401_REG_CFG2,
  68. ICS932S401_REG_CFG7,
  69. ICS932S401_REG_VENDOR_REV,
  70. ICS932S401_REG_DEVICE,
  71. ICS932S401_REG_CTRL,
  72. ICS932S401_REG_CPU_M_CTRL,
  73. ICS932S401_REG_CPU_N_CTRL,
  74. ICS932S401_REG_CPU_SPREAD1,
  75. ICS932S401_REG_CPU_SPREAD2,
  76. ICS932S401_REG_SRC_M_CTRL,
  77. ICS932S401_REG_SRC_N_CTRL,
  78. ICS932S401_REG_SRC_SPREAD1,
  79. ICS932S401_REG_SRC_SPREAD2,
  80. ICS932S401_REG_CPU_DIVISOR,
  81. ICS932S401_REG_PCISRC_DIVISOR,
  82. };
  83. /* How often do we reread sensors values? (In jiffies) */
  84. #define SENSOR_REFRESH_INTERVAL (2 * HZ)
  85. /* How often do we reread sensor limit values? (In jiffies) */
  86. #define LIMIT_REFRESH_INTERVAL (60 * HZ)
  87. struct ics932s401_data {
  88. struct attribute_group attrs;
  89. struct mutex lock;
  90. char sensors_valid;
  91. unsigned long sensors_last_updated; /* In jiffies */
  92. u8 regs[NUM_REGS];
  93. };
  94. static int ics932s401_probe(struct i2c_client *client,
  95. const struct i2c_device_id *id);
  96. static int ics932s401_detect(struct i2c_client *client,
  97. struct i2c_board_info *info);
  98. static int ics932s401_remove(struct i2c_client *client);
  99. static const struct i2c_device_id ics932s401_id[] = {
  100. { "ics932s401", 0 },
  101. { }
  102. };
  103. MODULE_DEVICE_TABLE(i2c, ics932s401_id);
  104. static struct i2c_driver ics932s401_driver = {
  105. .class = I2C_CLASS_HWMON,
  106. .driver = {
  107. .name = "ics932s401",
  108. },
  109. .probe = ics932s401_probe,
  110. .remove = ics932s401_remove,
  111. .id_table = ics932s401_id,
  112. .detect = ics932s401_detect,
  113. .address_list = normal_i2c,
  114. };
  115. static struct ics932s401_data *ics932s401_update_device(struct device *dev)
  116. {
  117. struct i2c_client *client = to_i2c_client(dev);
  118. struct ics932s401_data *data = i2c_get_clientdata(client);
  119. unsigned long local_jiffies = jiffies;
  120. int i, temp;
  121. mutex_lock(&data->lock);
  122. if (time_before(local_jiffies, data->sensors_last_updated +
  123. SENSOR_REFRESH_INTERVAL)
  124. && data->sensors_valid)
  125. goto out;
  126. /*
  127. * Each register must be read as a word and then right shifted 8 bits.
  128. * Not really sure why this is; setting the "byte count programming"
  129. * register to 1 does not fix this problem.
  130. */
  131. for (i = 0; i < NUM_MIRRORED_REGS; i++) {
  132. temp = i2c_smbus_read_word_data(client, regs_to_copy[i]);
  133. data->regs[regs_to_copy[i]] = temp >> 8;
  134. }
  135. data->sensors_last_updated = local_jiffies;
  136. data->sensors_valid = 1;
  137. out:
  138. mutex_unlock(&data->lock);
  139. return data;
  140. }
  141. static ssize_t show_spread_enabled(struct device *dev,
  142. struct device_attribute *devattr,
  143. char *buf)
  144. {
  145. struct ics932s401_data *data = ics932s401_update_device(dev);
  146. if (data->regs[ICS932S401_REG_CFG2] & ICS932S401_CFG1_SPREAD)
  147. return sprintf(buf, "1\n");
  148. return sprintf(buf, "0\n");
  149. }
  150. /* bit to cpu khz map */
  151. static const int fs_speeds[] = {
  152. 266666,
  153. 133333,
  154. 200000,
  155. 166666,
  156. 333333,
  157. 100000,
  158. 400000,
  159. 0,
  160. };
  161. /* clock divisor map */
  162. static const int divisors[] = {2, 3, 5, 15, 4, 6, 10, 30, 8, 12, 20, 60, 16,
  163. 24, 40, 120};
  164. /* Calculate CPU frequency from the M/N registers. */
  165. static int calculate_cpu_freq(struct ics932s401_data *data)
  166. {
  167. int m, n, freq;
  168. m = data->regs[ICS932S401_REG_CPU_M_CTRL] & ICS932S401_M_MASK;
  169. n = data->regs[ICS932S401_REG_CPU_N_CTRL];
  170. /* Pull in bits 8 & 9 from the M register */
  171. n |= ((int)data->regs[ICS932S401_REG_CPU_M_CTRL] & 0x80) << 1;
  172. n |= ((int)data->regs[ICS932S401_REG_CPU_M_CTRL] & 0x40) << 3;
  173. freq = BASE_CLOCK * (n + 8) / (m + 2);
  174. freq /= divisors[data->regs[ICS932S401_REG_CPU_DIVISOR] >>
  175. ICS932S401_CPU_DIVISOR_SHIFT];
  176. return freq;
  177. }
  178. static ssize_t show_cpu_clock(struct device *dev,
  179. struct device_attribute *devattr,
  180. char *buf)
  181. {
  182. struct ics932s401_data *data = ics932s401_update_device(dev);
  183. return sprintf(buf, "%d\n", calculate_cpu_freq(data));
  184. }
  185. static ssize_t show_cpu_clock_sel(struct device *dev,
  186. struct device_attribute *devattr,
  187. char *buf)
  188. {
  189. struct ics932s401_data *data = ics932s401_update_device(dev);
  190. int freq;
  191. if (data->regs[ICS932S401_REG_CTRL] & ICS932S401_MN_ENABLED)
  192. freq = calculate_cpu_freq(data);
  193. else {
  194. /* Freq is neatly wrapped up for us */
  195. int fid = data->regs[ICS932S401_REG_CFG7] & ICS932S401_FS_MASK;
  196. freq = fs_speeds[fid];
  197. if (data->regs[ICS932S401_REG_CTRL] & ICS932S401_CPU_ALT) {
  198. switch (freq) {
  199. case 166666:
  200. freq = 160000;
  201. break;
  202. case 333333:
  203. freq = 320000;
  204. break;
  205. }
  206. }
  207. }
  208. return sprintf(buf, "%d\n", freq);
  209. }
  210. /* Calculate SRC frequency from the M/N registers. */
  211. static int calculate_src_freq(struct ics932s401_data *data)
  212. {
  213. int m, n, freq;
  214. m = data->regs[ICS932S401_REG_SRC_M_CTRL] & ICS932S401_M_MASK;
  215. n = data->regs[ICS932S401_REG_SRC_N_CTRL];
  216. /* Pull in bits 8 & 9 from the M register */
  217. n |= ((int)data->regs[ICS932S401_REG_SRC_M_CTRL] & 0x80) << 1;
  218. n |= ((int)data->regs[ICS932S401_REG_SRC_M_CTRL] & 0x40) << 3;
  219. freq = BASE_CLOCK * (n + 8) / (m + 2);
  220. freq /= divisors[data->regs[ICS932S401_REG_PCISRC_DIVISOR] &
  221. ICS932S401_SRC_DIVISOR_MASK];
  222. return freq;
  223. }
  224. static ssize_t show_src_clock(struct device *dev,
  225. struct device_attribute *devattr,
  226. char *buf)
  227. {
  228. struct ics932s401_data *data = ics932s401_update_device(dev);
  229. return sprintf(buf, "%d\n", calculate_src_freq(data));
  230. }
  231. static ssize_t show_src_clock_sel(struct device *dev,
  232. struct device_attribute *devattr,
  233. char *buf)
  234. {
  235. struct ics932s401_data *data = ics932s401_update_device(dev);
  236. int freq;
  237. if (data->regs[ICS932S401_REG_CTRL] & ICS932S401_MN_ENABLED)
  238. freq = calculate_src_freq(data);
  239. else
  240. /* Freq is neatly wrapped up for us */
  241. if (data->regs[ICS932S401_REG_CTRL] & ICS932S401_CPU_ALT &&
  242. data->regs[ICS932S401_REG_CTRL] & ICS932S401_SRC_ALT)
  243. freq = 96000;
  244. else
  245. freq = 100000;
  246. return sprintf(buf, "%d\n", freq);
  247. }
  248. /* Calculate PCI frequency from the SRC M/N registers. */
  249. static int calculate_pci_freq(struct ics932s401_data *data)
  250. {
  251. int m, n, freq;
  252. m = data->regs[ICS932S401_REG_SRC_M_CTRL] & ICS932S401_M_MASK;
  253. n = data->regs[ICS932S401_REG_SRC_N_CTRL];
  254. /* Pull in bits 8 & 9 from the M register */
  255. n |= ((int)data->regs[ICS932S401_REG_SRC_M_CTRL] & 0x80) << 1;
  256. n |= ((int)data->regs[ICS932S401_REG_SRC_M_CTRL] & 0x40) << 3;
  257. freq = BASE_CLOCK * (n + 8) / (m + 2);
  258. freq /= divisors[data->regs[ICS932S401_REG_PCISRC_DIVISOR] >>
  259. ICS932S401_PCI_DIVISOR_SHIFT];
  260. return freq;
  261. }
  262. static ssize_t show_pci_clock(struct device *dev,
  263. struct device_attribute *devattr,
  264. char *buf)
  265. {
  266. struct ics932s401_data *data = ics932s401_update_device(dev);
  267. return sprintf(buf, "%d\n", calculate_pci_freq(data));
  268. }
  269. static ssize_t show_pci_clock_sel(struct device *dev,
  270. struct device_attribute *devattr,
  271. char *buf)
  272. {
  273. struct ics932s401_data *data = ics932s401_update_device(dev);
  274. int freq;
  275. if (data->regs[ICS932S401_REG_CTRL] & ICS932S401_MN_ENABLED)
  276. freq = calculate_pci_freq(data);
  277. else
  278. freq = 33333;
  279. return sprintf(buf, "%d\n", freq);
  280. }
  281. static ssize_t show_value(struct device *dev,
  282. struct device_attribute *devattr,
  283. char *buf);
  284. static ssize_t show_spread(struct device *dev,
  285. struct device_attribute *devattr,
  286. char *buf);
  287. static DEVICE_ATTR(spread_enabled, S_IRUGO, show_spread_enabled, NULL);
  288. static DEVICE_ATTR(cpu_clock_selection, S_IRUGO, show_cpu_clock_sel, NULL);
  289. static DEVICE_ATTR(cpu_clock, S_IRUGO, show_cpu_clock, NULL);
  290. static DEVICE_ATTR(src_clock_selection, S_IRUGO, show_src_clock_sel, NULL);
  291. static DEVICE_ATTR(src_clock, S_IRUGO, show_src_clock, NULL);
  292. static DEVICE_ATTR(pci_clock_selection, S_IRUGO, show_pci_clock_sel, NULL);
  293. static DEVICE_ATTR(pci_clock, S_IRUGO, show_pci_clock, NULL);
  294. static DEVICE_ATTR(usb_clock, S_IRUGO, show_value, NULL);
  295. static DEVICE_ATTR(ref_clock, S_IRUGO, show_value, NULL);
  296. static DEVICE_ATTR(cpu_spread, S_IRUGO, show_spread, NULL);
  297. static DEVICE_ATTR(src_spread, S_IRUGO, show_spread, NULL);
  298. static struct attribute *ics932s401_attr[] =
  299. {
  300. &dev_attr_spread_enabled.attr,
  301. &dev_attr_cpu_clock_selection.attr,
  302. &dev_attr_cpu_clock.attr,
  303. &dev_attr_src_clock_selection.attr,
  304. &dev_attr_src_clock.attr,
  305. &dev_attr_pci_clock_selection.attr,
  306. &dev_attr_pci_clock.attr,
  307. &dev_attr_usb_clock.attr,
  308. &dev_attr_ref_clock.attr,
  309. &dev_attr_cpu_spread.attr,
  310. &dev_attr_src_spread.attr,
  311. NULL
  312. };
  313. static ssize_t show_value(struct device *dev,
  314. struct device_attribute *devattr,
  315. char *buf)
  316. {
  317. int x;
  318. if (devattr == &dev_attr_usb_clock)
  319. x = 48000;
  320. else if (devattr == &dev_attr_ref_clock)
  321. x = BASE_CLOCK;
  322. else
  323. BUG();
  324. return sprintf(buf, "%d\n", x);
  325. }
  326. static ssize_t show_spread(struct device *dev,
  327. struct device_attribute *devattr,
  328. char *buf)
  329. {
  330. struct ics932s401_data *data = ics932s401_update_device(dev);
  331. int reg;
  332. unsigned long val;
  333. if (!(data->regs[ICS932S401_REG_CFG2] & ICS932S401_CFG1_SPREAD))
  334. return sprintf(buf, "0%%\n");
  335. if (devattr == &dev_attr_src_spread)
  336. reg = ICS932S401_REG_SRC_SPREAD1;
  337. else if (devattr == &dev_attr_cpu_spread)
  338. reg = ICS932S401_REG_CPU_SPREAD1;
  339. else
  340. BUG();
  341. val = data->regs[reg] | (data->regs[reg + 1] << 8);
  342. val &= ICS932S401_SPREAD_MASK;
  343. /* Scale 0..2^14 to -0.5. */
  344. val = 500000 * val / 16384;
  345. return sprintf(buf, "-0.%lu%%\n", val);
  346. }
  347. /* Return 0 if detection is successful, -ENODEV otherwise */
  348. static int ics932s401_detect(struct i2c_client *client,
  349. struct i2c_board_info *info)
  350. {
  351. struct i2c_adapter *adapter = client->adapter;
  352. int vendor, device, revision;
  353. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  354. return -ENODEV;
  355. vendor = i2c_smbus_read_word_data(client, ICS932S401_REG_VENDOR_REV);
  356. vendor >>= 8;
  357. revision = vendor >> ICS932S401_REV_SHIFT;
  358. vendor &= ICS932S401_VENDOR_MASK;
  359. if (vendor != ICS932S401_VENDOR)
  360. return -ENODEV;
  361. device = i2c_smbus_read_word_data(client, ICS932S401_REG_DEVICE);
  362. device >>= 8;
  363. if (device != ICS932S401_DEVICE)
  364. return -ENODEV;
  365. if (revision != ICS932S401_REV)
  366. dev_info(&adapter->dev, "Unknown revision %d\n", revision);
  367. strlcpy(info->type, "ics932s401", I2C_NAME_SIZE);
  368. return 0;
  369. }
  370. static int ics932s401_probe(struct i2c_client *client,
  371. const struct i2c_device_id *id)
  372. {
  373. struct ics932s401_data *data;
  374. int err;
  375. data = kzalloc(sizeof(struct ics932s401_data), GFP_KERNEL);
  376. if (!data) {
  377. err = -ENOMEM;
  378. goto exit;
  379. }
  380. i2c_set_clientdata(client, data);
  381. mutex_init(&data->lock);
  382. dev_info(&client->dev, "%s chip found\n", client->name);
  383. /* Register sysfs hooks */
  384. data->attrs.attrs = ics932s401_attr;
  385. err = sysfs_create_group(&client->dev.kobj, &data->attrs);
  386. if (err)
  387. goto exit_free;
  388. return 0;
  389. exit_free:
  390. kfree(data);
  391. exit:
  392. return err;
  393. }
  394. static int ics932s401_remove(struct i2c_client *client)
  395. {
  396. struct ics932s401_data *data = i2c_get_clientdata(client);
  397. sysfs_remove_group(&client->dev.kobj, &data->attrs);
  398. kfree(data);
  399. return 0;
  400. }
  401. static int __init ics932s401_init(void)
  402. {
  403. return i2c_add_driver(&ics932s401_driver);
  404. }
  405. static void __exit ics932s401_exit(void)
  406. {
  407. i2c_del_driver(&ics932s401_driver);
  408. }
  409. MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
  410. MODULE_DESCRIPTION("ICS932S401 driver");
  411. MODULE_LICENSE("GPL");
  412. module_init(ics932s401_init);
  413. module_exit(ics932s401_exit);
  414. /* IBM IntelliStation Z30 */
  415. MODULE_ALIAS("dmi:bvnIBM:*:rn9228:*");
  416. MODULE_ALIAS("dmi:bvnIBM:*:rn9232:*");
  417. /* IBM x3650/x3550 */
  418. MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3650*");
  419. MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3550*");