ics932s401.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*
  2. * A driver for the Integrated Circuits ICS932S401
  3. * Copyright (C) 2008 IBM
  4. *
  5. * Author: Darrick J. Wong <darrick.wong@oracle.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. &dev_attr_spread_enabled.attr,
  300. &dev_attr_cpu_clock_selection.attr,
  301. &dev_attr_cpu_clock.attr,
  302. &dev_attr_src_clock_selection.attr,
  303. &dev_attr_src_clock.attr,
  304. &dev_attr_pci_clock_selection.attr,
  305. &dev_attr_pci_clock.attr,
  306. &dev_attr_usb_clock.attr,
  307. &dev_attr_ref_clock.attr,
  308. &dev_attr_cpu_spread.attr,
  309. &dev_attr_src_spread.attr,
  310. NULL
  311. };
  312. static ssize_t show_value(struct device *dev,
  313. struct device_attribute *devattr,
  314. char *buf)
  315. {
  316. int x;
  317. if (devattr == &dev_attr_usb_clock)
  318. x = 48000;
  319. else if (devattr == &dev_attr_ref_clock)
  320. x = BASE_CLOCK;
  321. else
  322. BUG();
  323. return sprintf(buf, "%d\n", x);
  324. }
  325. static ssize_t show_spread(struct device *dev,
  326. struct device_attribute *devattr,
  327. char *buf)
  328. {
  329. struct ics932s401_data *data = ics932s401_update_device(dev);
  330. int reg;
  331. unsigned long val;
  332. if (!(data->regs[ICS932S401_REG_CFG2] & ICS932S401_CFG1_SPREAD))
  333. return sprintf(buf, "0%%\n");
  334. if (devattr == &dev_attr_src_spread)
  335. reg = ICS932S401_REG_SRC_SPREAD1;
  336. else if (devattr == &dev_attr_cpu_spread)
  337. reg = ICS932S401_REG_CPU_SPREAD1;
  338. else
  339. BUG();
  340. val = data->regs[reg] | (data->regs[reg + 1] << 8);
  341. val &= ICS932S401_SPREAD_MASK;
  342. /* Scale 0..2^14 to -0.5. */
  343. val = 500000 * val / 16384;
  344. return sprintf(buf, "-0.%lu%%\n", val);
  345. }
  346. /* Return 0 if detection is successful, -ENODEV otherwise */
  347. static int ics932s401_detect(struct i2c_client *client,
  348. struct i2c_board_info *info)
  349. {
  350. struct i2c_adapter *adapter = client->adapter;
  351. int vendor, device, revision;
  352. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  353. return -ENODEV;
  354. vendor = i2c_smbus_read_word_data(client, ICS932S401_REG_VENDOR_REV);
  355. vendor >>= 8;
  356. revision = vendor >> ICS932S401_REV_SHIFT;
  357. vendor &= ICS932S401_VENDOR_MASK;
  358. if (vendor != ICS932S401_VENDOR)
  359. return -ENODEV;
  360. device = i2c_smbus_read_word_data(client, ICS932S401_REG_DEVICE);
  361. device >>= 8;
  362. if (device != ICS932S401_DEVICE)
  363. return -ENODEV;
  364. if (revision != ICS932S401_REV)
  365. dev_info(&adapter->dev, "Unknown revision %d\n", revision);
  366. strlcpy(info->type, "ics932s401", I2C_NAME_SIZE);
  367. return 0;
  368. }
  369. static int ics932s401_probe(struct i2c_client *client,
  370. const struct i2c_device_id *id)
  371. {
  372. struct ics932s401_data *data;
  373. int err;
  374. data = kzalloc(sizeof(struct ics932s401_data), GFP_KERNEL);
  375. if (!data) {
  376. err = -ENOMEM;
  377. goto exit;
  378. }
  379. i2c_set_clientdata(client, data);
  380. mutex_init(&data->lock);
  381. dev_info(&client->dev, "%s chip found\n", client->name);
  382. /* Register sysfs hooks */
  383. data->attrs.attrs = ics932s401_attr;
  384. err = sysfs_create_group(&client->dev.kobj, &data->attrs);
  385. if (err)
  386. goto exit_free;
  387. return 0;
  388. exit_free:
  389. kfree(data);
  390. exit:
  391. return err;
  392. }
  393. static int ics932s401_remove(struct i2c_client *client)
  394. {
  395. struct ics932s401_data *data = i2c_get_clientdata(client);
  396. sysfs_remove_group(&client->dev.kobj, &data->attrs);
  397. kfree(data);
  398. return 0;
  399. }
  400. module_i2c_driver(ics932s401_driver);
  401. MODULE_AUTHOR("Darrick J. Wong <darrick.wong@oracle.com>");
  402. MODULE_DESCRIPTION("ICS932S401 driver");
  403. MODULE_LICENSE("GPL");
  404. /* IBM IntelliStation Z30 */
  405. MODULE_ALIAS("dmi:bvnIBM:*:rn9228:*");
  406. MODULE_ALIAS("dmi:bvnIBM:*:rn9232:*");
  407. /* IBM x3650/x3550 */
  408. MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3650*");
  409. MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3550*");